forked from BabesGotByte/Coding_SkillSet_Topicwise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer37.cpp
More file actions
33 lines (33 loc) · 661 Bytes
/
Answer37.cpp
File metadata and controls
33 lines (33 loc) · 661 Bytes
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
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head){
return NULL;
}
ListNode* temp=head;
int count=0;
while(temp){
temp=temp->next;
count++;
}
k=k%count;
if(k==0){
return head;
}
k=count-k;
k--;
temp=head;
ListNode* prev;
while(k--){
temp=temp->next;
}
prev=temp->next;
temp->next=NULL;
ListNode* q=prev;
while(prev->next){
prev=prev->next;
}
prev->next=head;
return q;
}
};