forked from timoncui/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotate_List.cpp
54 lines (45 loc) · 1.26 KB
/
Rotate_List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Author: Timon Cui, timonbaby@163.com
Title: Rotate List
Description:
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
Difficulty rating: Easy
Notes:
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if (head == NULL) return NULL;
if (k == 0) return head;
// Let tail walk k steps
ListNode *tail = head;
int L = 0;
while (tail->next && L < k) {
tail = tail->next;
L ++;
}
// If reaches the end before k steps, the list has length L + 1
if (L < k) return rotateRight(head, k % (L + 1));
// Keep walking parent and tail together till the end
ListNode *parent = head;
while (tail->next) {
tail = tail->next;
parent = parent->next;
}
tail->next = head;
head = parent->next;
parent->next = NULL;
return head;
}
};