-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_LL.cpp
94 lines (80 loc) · 1.78 KB
/
rotate_LL.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int data = 0){
this->val = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted: "<<this->val<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}
int length(Node* &head){
Node* temp = head;
int len = 0;
while(temp){
len++;
temp = temp->next;
}
return len;
}
Node* rotateLL(Node* head,int k){
if(head == NULL) return NULL;
int len = length(head);
if(k % len == 0) return head;
int newK = len - (k % len) - 1;
Node* temp = head;
while(newK--){
temp = temp->next;
}
Node* newHead = temp->next;
Node* p = newHead;
temp->next = NULL;
while (p->next)
p = p->next;
p->next = head;
return newHead;
}
int main()
{
Node* head = new Node(1);
Node* sec = new Node(2);
Node* third = new Node(3);
Node* forth = new Node(4);
Node* fifth = new Node(5);
Node* sixth = new Node(6);
Node* seventh = new Node(7);
Node* eighth = new Node(8);
Node* ninth = new Node(9);
Node* tenth = new Node(10);
head->next = sec;
sec->next = third;
third->next = forth;
forth->next = fifth;
fifth->next = sixth;
sixth->next = seventh;
seventh->next = eighth;
eighth->next = ninth;
ninth->next = tenth;
tenth->next = NULL;
int k = 6;
print(head);
Node* ans = rotateLL(head,6);
print(ans);
return 0;
}