-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0061-rotate-list.cpp
45 lines (43 loc) · 1.21 KB
/
0061-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
/*
Given the head of a linked list, rotate the list to the right by k places.
Example: list = [1,2,3,4,5] and k = 2
Output: [4,5,1,2,3]
Time complexity: O(n)
Space complexity: O(1)
*/
class Solution {
private:
int findLen(ListNode* head){
int len = 0;
while(head!=NULL){
len++;
head = head->next;
}
return len;
}
ListNode* findNewHead(ListNode* head,int k){
int i=0;
while(i+1<k){
i++;
head = head->next;
}
ListNode* ret = head->next;
head->next = NULL;
return ret;
}
ListNode* findLast(ListNode* head){
while(head->next!=NULL) head = head->next;
return head;
}
public:
ListNode* rotateRight(ListNode* head, int k) {
int len = findLen(head); // Finds the length of the Linked List
if(len==0) return head;
k = k%len;
if(k==0) return head;
ListNode* newHead = findNewHead(head,len-k); // Finds the node that is the new head
ListNode* last = findLast(newHead); // Finds the last node from the new head and connects it to the previous head
last->next = head;
return newHead;
}
};