-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotateLinkList.cpp
188 lines (165 loc) · 3.74 KB
/
rotateLinkList.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
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.
*/
#include "iostream"
using namespace std;
struct node
{
struct node* next;
int data;
};
node * head = NULL;
node * head1 = NULL;
void insert(node *temp, int data)
{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new node;
temp = temp->next;
temp->data = data;
temp->next = NULL;
}
void display(node* temp)
{
while(temp != NULL)
{
cout<<" "<<temp->data;
temp = temp->next;
}
cout<<endl;
}
/*
A=pointer to list
B=number of shift operation
*/
node* rotateRight(node* A, int B) {
//-ve check
if(A==NULL)
return NULL;
//calculate length of list
int length=1;
node*temp1= A;
while(temp1->next != NULL)
{
temp1 = temp1->next;
length++;
}
//if the list is having only single element then shifting right.=/left would remain same
if(length < 2)
return A;
B = B%length;
//if the list having shifting digit more than the length then remainder of B/length would be the shift require
if(B ==0 )
return A;
node *oldHead = A;
int i=1;//intially A pointing to head i.e i=1
//break the list
while(i<length-B)
{
i++;
A = A->next;
}
node *newHead= A->next, *temp = A->next;
A->next = NULL;
cout<<"\n newHead for right shift list "<<newHead->data<<endl;
while(temp->next !=NULL)
{
temp = temp->next;
}
temp->next = oldHead;
return newHead;
}
/*
A=pointer to list
B=number of shift operation
*/
node* rotateLeft(node* A, int B) {
//-ve check
if(A==NULL)
return NULL;
//calculate length of list
int length=1;
node*temp1= A;
while(temp1->next != NULL)
{
temp1 = temp1->next;
length++;
}
//if the list is having only single element then shifting right.=/left would remain same
if(length < 2)
return A;
B = B%length;
//if the list having shifting digit more than the length then remainder of B/length would be the shift require
if(B ==0 )
return A;
node *oldHead = A;
int i=1;//intially A pointing to head i.e i=1
//break the list
while(i<B)
{
i++;
A = A->next;
}
node *newHead= A->next, *temp = A->next;
A->next = NULL;
cout<<"\n newHead for left shift list "<<newHead->data<<endl;
while(temp->next !=NULL)
{
temp = temp->next;
}
temp->next = oldHead;
return newHead;
}
int main()
{
//list 1 to rotate right
head = new node;
head->data = 1;
head->next = NULL;
insert(head, 2);
insert(head, 3);
insert(head, 4);
insert(head, 5);
insert(head, 6);
insert(head, 7);
insert(head, 8);
insert(head, 9);
insert(head, 10);
//list2 to rotate left
head1 = new node;
head1->data = 1;
head1->next = NULL;
insert(head1, 2);
insert(head1, 3);
insert(head1, 4);
insert(head1, 5);
insert(head1, 6);
insert(head1, 7);
insert(head1, 8);
insert(head1, 9);
insert(head1, 10);
//both the list are same
cout<<" display the input list"<<endl;
display(head);
head = rotateRight(head,2);
cout<<" \n display the right shift list"<<endl;
display(head);
head1 = rotateLeft(head1,2);
cout<<" \n display the left shift list"<<endl;
display(head1);
}
/*
display the input list
1 2 3 4 5 6 7 8 9 10
newHead for right shift list 9
display the right shift list
9 10 1 2 3 4 5 6 7 8
newHead for left shift list 3
display the left shift list
3 4 5 6 7 8 9 10 1 2
*/