-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathRotate_LinkedList_K_Times.cpp
60 lines (58 loc) · 1.38 KB
/
Rotate_LinkedList_K_Times.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
55
56
57
58
59
60
#include<iostream>
using namespace std;
class ListNode {
public:
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode* rotateRight(ListNode* head, int k) {
if(head == NULL) return head;
int len = 0;
ListNode* temp = head;
while(temp!=NULL){
len++;
temp = temp->next;
}
ListNode* tail = head;
for(int i=1;i<len;i++){
tail = tail->next;
}
if(k == len) return head;
if(k>len) k%=len;
int kk = len-k;
temp = head;
for(int i=1;i<kk;i++){
temp = temp->next;
}
tail->next = head;
head = temp->next;
temp->next = NULL;
return head;
}
int main(){
ListNode* head = new ListNode(10);
int n;
cout<<"Enter value of n: ";
cin>>n;
cout<<"Enter value of ListNodes: \n";
ListNode* temp = head;
for(int i=0;i<n;i++){
int x;
cin>>x;
ListNode* t = new ListNode(x);
temp->next = t;
temp = temp->next;
}
head = head->next;
int k;
cout<<"Enter value of k: ";
cin>>k;
head = rotateRight(head,k);
while(head!=NULL){
cout<<head->val<<" ";
head = head->next;
}
}