Rotate List

MediumLinked Listtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the head of a singly linked list and a non-negative integer k, rotate the list to the right by k places and return the new head. One rotation moves the last node to the front. k can be much larger than the list length; rotating by the length leaves the list unchanged. An empty list stays empty for any k.

Examples

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
After one rotation: [5,1,2,3,4]. After two: [4,5,1,2,3].
Input: head = [0,1,2], k = 4
Output: [2,0,1]
Rotating by 3 returns the original list, so 4 rotations equal 1.

Constraints

  • 0 <= number of nodes <= 500
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems