[Java][Linked List][LeetCode] Rotate List #61

SP Hou
1 min readApr 16, 2021

--

Given the head of a linked list, rotate the list to the right by k places.

Example 1.
Input:
head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
Example 2.
Input:
head = [0,1,2], k = 4
Output: [2,0,1]

Constraints:

  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

============Solve===============
Finally…I solve last problem in Linked List theme.
step 1. traversal all node to get list length.
step 2. get k mod length, if mod is 0, that mean I don’t need to move any node.
step 3. track and point again from head, until mod count down to 0. this time, I want to get all node list after mod-th.
step 4. create a new node list and equal this node is track node. then I need to track this node point one by one, and append new node to track node.

--

--

No responses yet