61. Rotate List
Analysis:
- It is equivalent to finding the last k nodes and make them the head.
- If k > len(list), k = k % len(list)
Algorithm:
- let fast be k steps ahead of slow.
- forward both pointer until fast is the last node
- now slow is the pre of the last kth node
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
1 | Input: 1->2->3->4->5->NULL, k = 2 |
Example 2:
1 | Input: 0->1->2->NULL, k = 4 |
Soulution:
1 | class Solution: |