-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked List.cpp
More file actions
417 lines (370 loc) · 9.2 KB
/
Copy pathLinked List.cpp
File metadata and controls
417 lines (370 loc) · 9.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <bits/stdc++.h>
using namespace std;
//Linked List
//Syntax & Implementation
class node
{
public:
int data;
node* next;
node(int val)
{
data=val;
next=NULL;
}
};
// void insertAthead(node* &head,int val){
// node* newNode=new node(val);
// newNode->next=head;
// head=newNode;
// }
// void insert(node* &head,int val)
// {
// node* n=new node(val);
// if (head==NULL)
// {
// head=n;
// return;
// }
// node* temp=head;
// while(temp->next != NULL)
// {
// temp=temp->next;
// }
// temp->next=n;
// }
// void insertAtPosition(Node* &head,int pos,int data){
// if(pos==1){
// insertAthead(head,d);
// return;
// }
// node* temp=head;
// int cnt=1;
// while(cnt<(pos-1)){
// temp=temp->next;
// cnt++;
// }
// if(temp->next==NULL){
// insert(tail,d);
// return;
// }
// node* nodeToinsert=new node(d);
// nodeToinsert->next=temp->next;
// temp->next=nodeToinsert;
// }
// void display(node* head)
// {
// node* temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<"->";
// temp=temp->next;
// }
// cout<<"NULL"<<endl;
// }
// void deleteathead(node* &head)
// {
// node* todelete=head;
// head=head->next;
// delete todelete;
// }
// void deletion(node* &head,int val)
// {
// node* temp=head;
// while(temp->next->data != val)
// {
// temp=temp->next;
// }
// node* todelete=temp->next;
// temp->next=temp->next->next;
// delete todelete;
// }
//Delete at Position in a Singly Linked List
// void deleteNode(ListNode* &newhead,int n){
// if(newhead==NULL){
// return;
// }
// ListNode* temp=newhead;
// if(n==1){
// newhead=temp->next;
// delete temp;
// return;
// }
// int count=1;
// while(temp!=NULL && count<(n-1)){
// temp=temp->next;
// count++;
// }
// ListNode* next=temp->next->next;
// delete temp->next;
// temp->next=next;
// return;
//}
// Linear Doubly Linked List
// class Node
// {
// public:
// int data;
// Node* next;
// Node* prev;
// Node(int val)
// {
// this->data=val;
// this->next=NULL;
// this->prev=NULL;
// }
// };
// void insertatHead(Node* &head,int val){
// Node*n=new Node(val);
// n->next=head;
// if(head!=NULL){
// head->prev=n;
// }
// head=n;
// }
// void insertatTail(Node* &head,int val){
// if(head==NULL){
// insertatHead(head,val);
// return;
// }
// Node* n=new Node(val);
// Node* temp=head;
// while (temp->next!=NULL){
// temp=temp->next;
// }
// temp->next=n;
// n->prev=temp;
// }
// void display(Node* head){
// Node* temp=head;
// while(temp!=NULL){
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// // Till Now We Have Created Functions For Setting And The Displaying The Values Of Linear Doubly Linked List.
// //Now We Will Write Functions For Deleting.
// void deleteatHead(Node* &head){
// Node* todelete=head; // We Have Created A Pointer Pointing Head Node.
// head=head->next; //We Have Updated Head Position Here.
// head->prev=NULL;
// delete todelete; // Now After Updating Our Head We Delete The Previous Head.
// }
// // In The Following Function We Pass On The Positon And The Head.
// void deletion(Node* &head,int pos){
// if(pos==1){
// deleteatHead(head);
// return;
// }
// Node* temp=head;
// int count=1;
// while (temp!=NULL && count!=pos)
// {
// temp=temp->next;
// count++;
// }
// temp->prev->next=temp->next;
// if(temp->next!=NULL){
// temp->next->prev=temp->prev;
// }
// delete temp;
// //We Created A temproray Pointer And Iterated To The Positon Whose Element Has To Be Deleted. Now The Previous Of The Temprorary
// //Pointer Will Point To The Next Of The Temprorary Pointer And Vice Versa For The Next Of The Temprorary Pointer. After
// //Updating We Deleted The Temprorary Pointer.
// }
// int main(){
// Node* head=NULL;
// insertatTail(head,3);
// insertatTail(head,4);
// insertatTail(head,5);
// display(head);
// insertatHead(head,2);
// insertatHead(head,1);
// display(head);
// deletion(head,2);
// display(head);
// return 0;
// }
// // Circular Linked List
// // For Adding A Node In The End We Make A Node And Than Traverse The
// // List To The Last Node Than Point The Next Of The Last Node To The
// // New Node And Next Of The New Node To The Head Node.
// class node{
// public:
// int data;
// node* next;
// node(int val){
// data=val;
// next=NULL;
// }
// };
// void insertatHead(node* &head,int val){
// node* n=new node(val);
// if(head==NULL){
// n->next=n;
// head=n;
// return;
// }
// node* temp=head;
// while(temp->next!=head){
// temp=temp->next;
// }
// temp->next=n;
// n->next=head;
// head=n;
// }
// void insertatTail(node* &head,int val){
// if(head==NULL){
// insertatHead(head,val);
// return;
// }
// node* n=new node(val);
// node* temp=head;
// while(temp->next!=head){
// temp=temp->next;
// }
// temp->next=n;
// n->next=head;
// }
// void deleteatHead(node* &head){
// node* temp=head;
// while(temp->next!=head){
// temp=temp->next;
// }
// node* todelete=head;
// temp->next=head->next;
// head=head->next;
// delete todelete;
// }
// void deletion( node* &head, int pos){
// if(pos==1){
// deleteatHead(head);
// return;
// }
// node* temp=head;
// int count=1;
// while(count!=pos-1){
// temp=temp->next;
// count++;
// }
// node* todelete=temp->next;
// temp->next=temp->next->next;
// delete todelete;
// }
// void display(node* head){
// node* temp=head;
// do
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// } while(temp!=head); // If We Put NULL In Place Of Head Than We Will Never Get Out Of This Loop
// cout<<endl;
// }
// int main(){
// node* head=NULL;
// insertatTail(head,1);
// insertatTail(head,2);
// insertatTail(head,3);
// insertatTail(head,4);
// display(head);
// insertatHead(head,5);
// display(head);
// deletion(head,5);
// display(head);
// return 0;
// }
//Middle of Linked List
//Non Optimized
// int lenOfLL(node* head){
// int len=0;
// while(head!=NULL){
// len++;
// head=head->next;
// }
// return len;
// }
// // node* findMiddle(node* head){
// // int len=lenOfLL(head);
// // int ans=len/2;
// // node* temp=head;
// // int cnt=0;
// // while(cnt<ans){
// // temp=temp->next;
// // cnt++;
// // }
// // return temp;
// // }
// //Optimized
// ListNode* findMiddle(ListNode* head){
// if(head==NULL || head->next==NULL){
// return head;
// }
// if(head->next->next==NULL){
// return head->next;
// }
// ListNode* slow=head;
// ListNode* fast=head->next;
// while(fast!=NULL){
// fast=fast->next;
// if(fast!=NULL){
// fast=fast->next;
// }
// slow=slow->next;
// }
// return slow;
// }
// // Reversing A Linked List
// // Iterative Way
// node* reverse(node* head)
// {
// node* prevptr=NULL;
// node* currptr=head;
// node* nextptr; //A pointer of node* i.e, poiting to a
// //data strucuture of type node.
// while(currptr!=NULL)
// {
// nextptr=currptr->next;
// currptr->next=prevptr;
// prevptr=currptr;
// currptr=nextptr;
// }
// return prevptr;
// }
//Recursive Way
// ListNode* reverseRecursive(ListNode* &head)
// {
// if (head==NULL || head->next==NULL)
// {
// return head;
// }
// ListNode* newhead=reverseRecursive(head->next);
// head->next->next=head;
// head->next=NULL;
// return newhead;
// }
//Remove Nth Node from The end of the linked list
// ListNode* removeNthFromEnd(ListNode* head,int n){
// ListNode* newHead=reverseRecusrsive(ListNode* head);
// deleteNode(newHead,n);
// ListNode* header=reverseRecusrsive(newHead);
// return header;
// }
//Reorder List
// void reorderList(ListNode* head) {
// ListNode* middleNode=findMiddle(head);
// ListNode* second=reverseRecursive(middleNode->next);
// middleNode->next=NULL;
// ListNode* first=head;
// while(second){
// //Keep Track of List
// ListNode* firstNext=first->next;
// ListNode* secondNext=second->next;
// //Change Links
// first->next=second;
// second->next=firstNext;
// //Increment first and second
// first=firstNext;
// second=secondNext;
// }
// }