LeetCode — Rotate List

Steven Lu
1 min readOct 7, 2020

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Solution:

There are 3 steps of what the following codes do:

  1. Find the length of linked list
  2. Determine where the head node would move to (length — k % length)th node
  3. Continue to perform rotation until it reaches the right node position

--

--