-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSingly_LL.cpp
735 lines (484 loc) · 11.8 KB
/
Singly_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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include<iostream>
//SINGLY LINKED LIST
using namespace std;
struct Node {
int data;
struct Node *next;
};
/*BASIC OPERATIONS- 1)INSERTION-Insertion at beginning-O(1) , insertions at end-O(n) , insertion in middle-O(n)
2)DELETION-front,tail and intermediate positions
3)TRAVERSAL -O(n)
*/
//Function to traverse the list and return the count of items in LL
struct Node *head = NULL;
int listlength(struct Node *head) {
struct Node *current = head;
int count = 0 ;
while(current!=NULL) {
count++;
cout<<current->data<<" | "<<"\t";
current=current->next;
}
return count;
}
//recursive function to traverse the list-similar to traversing the tree
void traverseRec(struct Node *head)
{
if(head!=NULL)
{
cout<<head->data<<"-->";
traverseRec(head->next);
}
else {
return;
}
}
//iterative solution-returns the new updated head of reversed list
Node* reverseList() {
Node *prev=NULL,*curr,*nextnode;
curr=head;
while(curr!=NULL) {
nextnode=curr->next;
curr->next=prev;
prev=curr;
curr=nextnode;
}
head=prev;
return head;
}
void InsertList(int data, int pos) {
struct Node *temp,*temp1; //temp pointers to be used in operations
struct Node *new_node = new Node(); //creating a new Node dynmically at runtime and allocating memory in HEAP using new() function
int k=1;
temp=head;
new_node->data = data;
if(pos==1) { //insertion at beginning- update the head and next pointer of new_node
new_node->next=temp;
head=new_node;
}
else {
//this condition will work for both insertion at middle and insertion at last
while((temp!=NULL) && (k<pos) ){
k++;
temp1=temp; //temp1 is the node previous to temp node
temp=temp->next;
}
//more optimal way-
temp1->next=new_node;
new_node->next=temp;
}
}
//only for deletion at beginning -O(1)
int DelFront() {
Node *temp=head;
head=head->next;
return temp->data;
delete(temp); //disposing temp
}
//function to delete last node -O(n)
int DelLast() {
Node *prev=head,*tail;
while(prev->next->next!=NULL) {
prev = prev->next; //prev is the 2nd last node
}
tail=prev->next;
prev->next=NULL;
return tail->data;
delete(tail);
}
//function to delete at an intermediate position
int DelPos(int pos) {
Node *prev,*del_node;
prev=head;
//loop to keep track of the prev node to the node to be deleted to adjust the next pointer of prev node
for(int i=1 ; i < pos-1 ; i++)
{
prev=prev->next;
}
//or
del_node=prev->next;
prev->next=del_node->next;
//prev->next=curr->next;
return del_node->data;
delete(del_node);//deallocating memory
}
//a single function to handle all delete cases
int DelNodePos(int pos) {
Node *temp=head;
Node *prev;
// cout<<"Enter position to delete: "<<endl;
// cin>>pos;
if(pos<=0) {
cout<<"Enter a valid index"<<endl;
return 0;
}
if(pos==1) {
head = head->next; //head points to 2nd node
}
//case which will handle all other posotions and the last node deletion too
else {
int k = 1;
while ((temp!=NULL) && (k<pos)) {
k++;
prev = temp;
temp=temp->next;
}
//updating the next pointer of prev node
prev->next=temp->next;
}
return temp->data;
delete(temp);
}
//a function to remove duplicates from a sorted linked list
Node* RemoveDuplicates(Node *head) //O(n)-TIME COMPLEXITY
{
// This is a "method-only" submission.
// You only need to complete this method.
Node *temp;
Node *temp1;
//traversing and finding duplicate elements in list
temp=head;
while(temp->next!=NULL) {
temp1=temp->next;
if(temp->data == temp1->data) {
temp->next = temp1->next;
delete(temp1);
}
else {
temp=temp->next;
}
}
return head;
}
//a function to remove a duplicate node from an unsorted List
void RemoveUnsortedDup(struct Node *head) {
Node *temp = head;
Node *temp1;
//traversing the list
while(temp->next!=NULL) {
temp1 = temp;
while(temp1->next!=NULL)
{
//if duplicate is found in the next position, then delete it
if(temp->data == temp1->next->data)
{
Node *dup = temp1->next;
temp1->next = dup->next;
delete(dup);
}
//otherwise simply mode temp1 to next node
else {
temp1=temp1->next;
}
}
temp=temp->next;
}
}// TIME COMPLEXITY-O(N^2)
//list should be sorted
bool has_cycle(Node* head) {
// Complete this function
// Do not write the main method
Node *temp=head;
Node *temp1=head;//temp1 is a node which is always ahead of temp
if(head==NULL){
return false;
}
//traversing the list and checking for cycles
while(temp->next!=NULL && temp1!=NULL){
temp=temp->next;
temp1=temp1=->next->next; //one node ahead
//if temp and temp1 pointers have same addresses
if(temp1==temp) {
cout<<"Has a cycle"<<endl;
return true;
}
}
return false;
}
//function to return the starting node of the cycle
Node *detectCycle(Node *head) {
struct Node *slow = head, *fast = head ;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
//if cycle exists i.e fast = slow
if(slow==fast)
{
slow = head;
while(slow!=fast)
{
slow = slow->next;
fast= fast->next;
}
return slow;
}
}
return NULL;
}
//function to insert in a Sorted linked list
void sortedInsert(struct Node** head,int data) {
struct Node *new_node = new Node();//allocating memory
struct Node *curr = *head;
new_node->data = data;
//case1 if list is empty or if new_node has smaller data then one at head of LL
if( *head==NULL) {
new_node->next = *head;
*head = new_node;
}
//insertion at beginning of list
else if( (*head)->data > data )
{
new_node->next = *head; //or curr
*head = new_node;
}
//CASE-3: 2 methods, when data > curr->data
else {
//METHOD-1
struct Node *prev = NULL;
while(curr->next!=NULL && curr->data <= data) {
prev = curr;
curr=curr->next;
}
//inssertion at tail of linked list
//Important to add curr->data <= data ,otherwise will not insert largest node at end
if(curr->next == NULL && curr->data <= data) {
new_node->next = NULL;
curr->next = new_node;
}
//else insertion between prev and curr
else {
prev->next = new_node;
new_node->next = curr;
}
//METHOD-2
//we traverse the list only if new node has larger data
/* LOGIC
find the appropriate node after
which the input node (let 9) is to be inserted.
To find the appropriate node start from head,
keep moving until you reach a node GN (10 in
the below diagram) who's value is greater than
the input node.
*/
/* Locate the node before the point of insertion */
// while(curr->next!=NULL && curr->next->data < data) {
//
// curr=curr->next;
// }
//
// //new node has larger data then curr , so new node will be inserted after curr node
// new_node->next = curr->next;
// curr->next = new_node;
}
}
//finding length of a List
int length(struct Node *head) {
int len = 0;
while(head!=NULL) {
len++;
head = head->next;
}
return len;
}
//function to print kth element from the tail-IMP
int printfromTail(Node *head,int postail) {
Node *temp=head;
Node *result=head; //a pointer to store the address of kth element from tail
int k=0;
//traversing the list
while(temp!=NULL) {
if(k++ > postail) {
result=result->next;
}
temp=temp->next;
}
return result->data;
}
//function to get Nth node in a list
int getNode(struct Node *head,int pos)
{
Node *temp = head;
int len = length(head); //finding length of list to check for corner cases
if(pos > len || pos < 0 ) {
cout<<"Invalid position"<<endl;
return 0;
}
else {
struct Node *temp1=head;
int k=0;
while(temp!=NULL)
{
if( ++k < pos) {
temp1=temp1->next;
}
temp = temp->next;
}
return temp1->data;
}
}
//TIME COMPLEXITY = O(n) of traversing the list
//function to get the Middle of a list
int MiddleNode(struct Node **head,int start=0)
{
if(*head==NULL)
{
cout<<"list enpty"<<endl;
}
int len = length(*head);
int mid = (start + (len-start)/2 );
struct Node *temp=*head;
for(int i=0;i<mid;i++)
temp=temp->next;
return temp->data;
}
int countOccUsingRecursion(Node *head,int data)
{
// if(!head) return 0;
//need to use static variable- to maintain 1 copy of count
static int count = 0 ;
if(head)
{
if((head)->data==data)
{
count++;
}
//otherwise recursively traverse the list and check for matches and increment count
countOccUsingRecursion(head->next,data);
}
return count;
}
void CountOccurence(struct Node **head,int data)
{
int count = 0 ;
Node *temp=*head;
if(*head==NULL) {
return ;
}
while(temp!=NULL)
{
if(temp->data == data)
{
count += 1 ;
}
temp = temp->next;
}
//if after traversing count not changed , then not found
if(count==0) {
cout<<"Not found ."<<endl;
return;
}
cout<<count;
}
Node *ReverseListRec(Node *head)
{
struct Node *new_head;
//base condition
if(head->next==NULL)
{
new_head = head;
}
//simply travese the list recursively until we reach the second last node
ReverseListRec(head->next);//
struct Node *prev;
prev = head->next;
prev->next = head;
head->next=NULL;
return new_head;
}
//recursive program to print a list in reverse fashion-similar to postorder traverasl.
//we first recur till end of list , and then simply print
void RecReversePrint(Node *head)
{
if(head==NULL) return;
//rec till end of list
RecReversePrint(head->next);
//then start printing
cout<<head->data<<"-->";
}
//recursive program to traverse a list
void TraverseRecursive(Node *head)
{
if(head==NULL) return;
//first print the data
cout<<head->data<<"-->";
//rec till end of list
TraverseRecursive(head->next);
}
int main() {
//allocating memory in heap;
// struct Node *n1 = new Node();
// struct Node *n2 = new Node();
// struct Node *n3 = new Node();
// struct Node *n4 = new Node();
//
//
// n1->data=2;
// n1->next=n2;
// n2->data=3;
// n2->next = n3;
// n3->data=4;
// n3->next = n4;
// n4->data = 5;
// n4->next = NULL;
//cout<<printfromTail(head,0);
sortedInsert(&head,10);
sortedInsert(&head,9);
sortedInsert(&head,9);
sortedInsert(&head,20);
sortedInsert(&head,2);
sortedInsert(&head,51);
sortedInsert(&head,9);
sortedInsert(&head,6);
sortedInsert(&head,61);
sortedInsert(&head,10);
sortedInsert(&head,60);
traverseRec(head);
cout<<endl;
RecReversePrint(head);
cout<<endl;
traverseRec(head);
// ReverseListRec(head);
// cout<<printfromTail(head,3)<<endl;
// cout<<getNode(head,3)<<endl;
//
// cout<<listlength(head)<<endl;
//
// cout<<endl;
// CountOccurence(&head,9);
//
// cout<<endl;
//
// cout<<countOccUsingRecursion(head,9);
//
// cout<<listlength(n1)<<endl;
//removing duplicates form unsorted list
// RemoveUnsortedDup(head);
// cout<<"After duplicate removal"<<endl;
// cout<<listlength(head)<<endl;
//
// cout<<"\n";
//
// //removing duplicates in a sorted list
// RemoveDuplicates(head);
//
//
// cout<<listlength(head)<<endl;
// has_cycle(head);
// cout<<printfromTail(head,1);
//
// cout<<"\n";
// cout<<endl;
//// cout<<DelPos(2)<<endl;
//
// cout<<DelNodePos(1)<<endl;
//
//
// reverseList();
//
// cout<<"List reversed:"<<endl;
//
//
// cout<<listlength(head);
return 0;
}