-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinked List.cpp
More file actions
1308 lines (1198 loc) · 31.3 KB
/
Linked List.cpp
File metadata and controls
1308 lines (1198 loc) · 31.3 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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
LINKED LIST:
DELETE ALTERNATE NODES
CHECKING CIRCULAR LINKED LIST
DELETE NODE IN DOUBLY LINKED LIST
IMPLEMENT STACK IN LINKED LIST
DELETE WITHOUT HEAD POINTER
MULTI -LINKED LIST
REVERSE DOUBLY LINKED LIST
DELETE MIDDLE OF LINKED LIST
REVERSE A LINKED LIST
REMOVE DUPLICATES (UNSORTED)
FIND LENGTH OF LOOP
INSERT IN A SORTED LINKED LIST
PAIRWISE SWAP ELEMENTS OF LINKED LIST
DELETION AND REVERSE IN CIRCULAR LINKED LIST
FUNCTION TO REVERSE A CIRCULAR LINKED LIST
SORTED INSERTION CIRCULAR LINKED LIST
CHECK IF LINKED LIST IS PALINDROME
DELETE N NODES AFTER M NODES OF LINKED LIST
ADD TWO NUMBERS
LINKED LIST IN ZIGZAG FASHION
QUICKSORT FOR LINKED LIST
INTERSECTION OF LINKED LIST(Y-SHAPED)
ROTATE A LINKED LIST
REMOVE LOOP IN LINKED LIST
REVERSE ALTERNATE NODES IN LINKED LIST
FLATTENING OF LINKED LIST
INSERTION SORT FOR LINKED LIST
SWAP KTH NODE FROM BEGINNING AND END
REVERSE A LINKED LIST IN GROUPS OF GIVEN SIZE
REVERSE A SUBLIST OF A LINKED LIST
CLONE A LINKED LIST WITH NEXT AND RANDOM POINTER
MERGE K SORTED LINKED LISTS
/*
Delete Alternate Nodes
Basic Accuracy: 51.7% Submissions: 37885 Points: 1
Given a Singly Linked List of size N, delete all alternate nodes of the list.
Example 1:
Input:
LinkedList: 1->2->3->4->5->6
Output: 1->3->5
Explanation: Deleting alternate nodes
results in the linked list with elements
1->3->5.
*/
void deleteAlt(struct Node *head){
if(!head || !head->next){
return;
}
Node*ptr=head;
while( ptr && ptr->next!=NULL){
ptr->next=ptr->next->next;
ptr=ptr->next;
} // Code here
}
/*
Check If Circular Linked List
Basic Accuracy: 50.74% Submissions: 61530 Points: 1
Given head, the head of a singly linked list, find if the linked list is circular or not.
A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle.
An empty linked list is considered as circular.
Example 1:
Input:
LinkedList: 1->2->3->4->5
(the first and last node is connected,
i.e. 5 --> 1)
Output: 1
*/
bool isCircular(struct Node *head){
if(!head)return true;
struct Node*temp=head;
while(temp!=NULL){
temp=temp->next;
if(temp==head){
return true;
}
}
return false;
//code here
}
/*
Delete node in Doubly Linked List
Basic Accuracy: 48.28% Submissions: 35329 Points: 1
Given a doubly linked list and a position. The task is to delete a node from given position in a doubly linked list.
Example 1:
Input:
LinkedList = 1 <--> 3 <--> 4
x = 3
Output: 1 3
Explanation: After deleting the node at
position 3 (position starts from 1),
the linked list will be now as 1->3.
*/
Node* deleteNode(Node *head, int x)
{
if(head==NULL)return head;
if(head->next==NULL)return NULL;
if(x==1){
head->next->prev=NULL;
head=head->next;
return head;
}
else{
Node*temp=head;
for(int i=0;i<x-2;i++){
temp=temp->next;
}
Node*temp2=temp->next;
if(temp2->next==NULL){
temp->next=NULL;
}
else{
temp->next=temp2->next;
temp2->next->prev=temp;
}
free(temp2);
return head;
}
//Your code here
}
/*
Implement Stack using Linked List
Basic Accuracy: 49.96% Submissions: 57521 Points: 1
Let's give it a try! You have a linked list and you have to implement the functionalities push and pop of stack using this given linked list.
Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
*/
void MyStack ::push(int x)
{
StackNode*temp=new StackNode(x);
temp->next=top;
top=temp;
// Your Code
}
//Function to remove an item from top of the stack.
int MyStack ::pop()
{if(top==NULL)return -1;
int x=top->data;
top=top->next;
return x;
// Your Code
}
/*
Delete without head pointer
Easy Accuracy: 69.01% Submissions: 100k+ Points: 2
You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Pointer/ reference to head node is not given.
Note: No head reference is given to you. It is guaranteed that the node to be deleted is not a tail node in the linked list.
Example 1:
Input:
N = 2
value[] = {1,2}
node = 1
Output: 2
Explanation: After deleting 1 from the
linked list, we have remaining nodes
as 2.
*/
void deleteNode(Node *del)
{
Node*temp=del->next;
del->data=temp->data;
del->next=temp->next;
// Your code here
}
/*
Given a Matrix mat of N*N size, the task is to complete the function constructLinkedMatrix(),
that constructs a 2D linked list representation of the given matrix.
Input : 2D matrix
1 2 3
4 5 6
7 8 9
Output :
1 -> 2 -> 3 -> NULL
| | |
v v v
4 -> 5 -> 6 -> NULL
| | |
v v v
7 -> 8 -> 9 -> NULL
| | |
v v v
NULL NULL NULL
*/
Node*solve(int mat[MAX][MAX],int i,int j,int n){
if(i>=n || j>=n)return NULL;
Node*res=new Node(mat[i][j]);
res->right=solve(mat,i,j+1,n);
res->down=solve(mat,i+1,j,n);
return res;
}
Node* constructLinkedMatrix(int mat[MAX][MAX], int n)
{
return solve(mat,0,0,n);
}
//Rotate doubly Linked List
struct node*update(struct node*start,int p)
{
node*temp=start;
node*hex=start;
for(int i=0;i<p-1;i++){
temp=temp->next;
}
start=temp->next;
start->prev=NULL;
temp->next=NULL;
node*q=start;
while(q->next!=NULL){
q=q->next;
}
hex->prev=q;
q->next=hex;
return start;
}
//REVERSE DOUBLY LINKED LIST
Node* reverseDLL(Node * head)
{
if(head==NULL || head->next==NULL)
return head;
Node*curr=head;
Node*pre=NULL;
Node*nex=head->next;
while(curr!=NULL){
curr->prev=curr->next;
curr->next=pre;
pre=curr;
curr=curr->prev;
}
return pre;
}
//Delete Middle of Linked List
Node* deleteMid(Node* head)
{
if(head==NULL || head->next==NULL)
return NULL;
Node*slow=head;
Node*fast=head;
Node*prev=NULL;
while(fast && fast->next){
prev=slow;
slow=slow->next;
fast=fast->next->next;
}
prev->next=slow->next;
return head;
}
//Reverse a linked list
struct Node* reverseList(struct Node *head)
{
if(!head && !head->next)return head;
Node*curr=head;
Node*nex=head;
Node*prev=NULL;
while(curr!=NULL){
nex=curr->next;
curr->next=prev;
prev=curr;
curr=nex;
}
return prev;
}
/*
Remove duplicates from an unsorted linked list
Easy Accuracy: 49.19% Submissions: 84542 Points: 2
Given an unsorted linked list of N nodes. The task is to remove duplicate elements from this unsorted Linked List.
When a value appears in multiple nodes, the node which appeared first should be kept, all others duplicates are to be removed.
Example 1:
Input:
N = 4
value[] = {5,2,2,4}
Output: 5 2 4
Explanation:Given linked list elements are
5->2->2->4, in which 2 is repeated only.
So, we will delete the extra repeated
elements 2 from the linked list and the
resultant linked list will contain 5->2->4
Example 2:
Input:
N = 5
value[] = {2,2,2,2,2}
Output: 2
Explanation:Given linked list elements are
2->2->2->2->2, in which 2 is repeated. So,
we will delete the extra repeated elements
2 from the linked list and the resultant
linked list will contain only 2.
*/
Node * removeDuplicates( Node *head)
{ if(head->next==NULL)
return head;
unordered_map<int,int>mp;
Node*temp=head;
Node*pre=NULL;
while(temp!=NULL){
if(mp.find(temp->data)!=mp.end()){
pre->next=temp->next;
}
else{
mp[temp->data]++;
pre=temp;
}
temp=temp->next;
}
return head;
}
/*Find length of Loop
Easy Accuracy: 43.66% Submissions: 61315 Points: 2
Given a linked list of size N.
The task is to complete the function countNodesinLoop() that
checks whether a given Linked List contains a loop or not and if the loop is present then return the count of nodes in a loop or else return 0.
C is the position of the node to which the last node is connected. If it is 0 then no loop.
Example 1:
Input:
N = 10
value[]={25,14,19,33,10,21,39,90,58,45}
C = 4
Output: 7
Explanation: The loop is 45->33. So
length of loop is 33->10->21->39->
90->58->45 = 7. The number 33 is
connected to the last node to form the
loop because according to the input the
4th node from the beginning(1 based
index) will be connected to the last
node for the loop.*/
int countNodesinLoop(struct Node *head)
{
Node*slow=head;
Node*fast=head;
int flag=0;
while(slow && fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
flag=1;
break;
}
}
if(flag==0){
return 0;
}
int l=1;
slow=slow->next;
while(slow!=fast){
l++;
slow=slow->next;
}
return l;
}
/*//Insert in a Sorted List
Easy Accuracy: 43.83% Submissions: 37881 Points: 2
Given a linked list sorted in ascending order and an integer called data, insert data in the linked list such that the list remains sorted.
Example 1:
Input:
LinkedList: 25->36->47->58->69->80
data: 19
Output: 19 25 36 47 58 69 80
*/
Node *sortedInsert(struct Node* head, int data) {
Node*temp=new Node(data);
Node*curr=head;
Node*prev=NULL;
if(head==NULL|| head->data>data){
temp->next=head;
head=temp;
return head;
}
else
{
while (curr->next != NULL and curr->next->data < temp->data)
{
curr = curr->next;
}
temp->next = curr->next;
curr->next = temp;
return head;
}
/*
Pairwise swap elements of a linked list
Easy Accuracy: 43.06% Submissions: 66985 Points: 2
Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.
Example 1:
Input:
LinkedList: 1->2->2->4->5->6->7->8
Output: 2 1 4 2 6 5 8 7
Explanation: After swapping each pair
considering (1,2), (2, 4), (5, 6).. so
on as pairs, we get 2, 1, 4, 2, 6, 5,
8, 7 as a new linked list.
*/
Node* pairWiseSwap(struct Node* head)
{
if(head==NULL ||head->next==NULL){
return head;
}
Node*temp1=head;
Node*temp2=temp1->next;
Node*temp3;
Node*prev=NULL;
Node*modified =temp2;
while(temp1!=NULL && temp1->next!=NULL){
temp2=temp1->next;
temp3=temp2->next;
temp2->next=temp1;
temp1->next=temp3;
if(prev!=NULL){
prev->next=temp2;
}
prev=temp1;
temp1=temp3;
}
return modified;
}
//Deletion and Reverse in Circular Linked List
void deleteNode(struct Node **head, int key)
{
Node*curr=*head;
if((*head)->data==key){
Node*temp=*head;
while(temp->next!=*head){
temp=temp->next;
}
Node*t=(*head)->next;
temp->next=(*head)->next;
*head=t;
}
while(curr->next!=*head && curr->data!=key){
pre=curr;
curr=curr->next;
}
//cout<<pre->data<<endl;
//cout<<curr->data<<endl;
pre->next=curr->next;
}
/* Function to reverse the linked list */
void reverse(struct Node** head_ref)
{
if (*head_ref == NULL)
return;
Node* prev = NULL;
Node* current = *head_ref;
Node* next;
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
} while (current != (*head_ref));
(*head_ref)->next = prev;
*head_ref = prev;
// Your code goes here
}
//Sorted insert for circular linked list
Node *sortedInsert(Node* head, int data)
{
Node*ptr=new Node(data);
if(head==NULL){
return ptr;
}
else if(head->data>ptr->data){
Node*curr=head;
while(curr->next!=head){
curr=curr->next;
}
curr->next=ptr;
ptr->next=head;
head=ptr;
return head;
}
else{
Node*p=head;
while(p->next->data<ptr->data){
p=p->next;
}
ptr->next=p->next;
p->next=ptr;
return head;
}
/*
Check if Linked List is Palindrome
Easy Accuracy: 39.25% Submissions: 100k+ Points: 2
Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Example 1:
Input:
N = 3
value[] = {1,2,1}
Output: 1
Explanation: The given linked list is
1 2 1 , which is a palindrome and
Hence, the output is 1.
*/
bool isPalindrome(Node *head)
{ int length=0;
Node*curr=head;
while(curr!=NULL){
length++;
curr=curr->next;
}
stack<int>s;
Node*slow=head;
Node*fast=head;
while(slow && fast && fast->next){
s.push(slow->data);
slow=slow->next;
fast=fast->next->next;
}
if(length%2==1)
slow=slow->next;
while(slow!=NULL){
if(slow->data==s.top()){
s.pop();
slow=slow->next;
}
else{
return 0;
}
}
return 1;
}
/*
Delete N nodes after M nodes of a linked list
Easy Accuracy: 37.19% Submissions: 34904 Points: 2
Given a linked list, delete N nodes after skipping M nodes of a linked list until the last of the linked list.
Input:
First line of input contains number of testcases T.
For each testcase, first line of input contains number of elements in the linked list and next M and N respectively space separated.
The last line contains the elements of the linked list.
Output:
Function should not print any output to stdin/console.
User Task:
The task is to complete the function linkdelete() which should modify the linked list as required.
Example:
Input:
2
8
2 1
9 1 3 5 9 4 10 1
6
6 1
1 2 3 4 5 6
Output:
9 1 5 9 10 1
1 2 3 4 5 6
*/
void linkdelete(struct Node *head, int M, int N)
{
unsigned int m=M-1, n=N;
Node *itr=head;
while(itr)
{
if(!m)
{
Node *temp=itr;
while(n-- && temp)
temp=temp->next;
if(itr && temp)
itr->next=temp->next;
else
itr->next=nullptr;
n=N;
m=M;
}
m--;
itr=itr->next;
}
}
/*
Add two numbers represented by linked lists
Easy Accuracy: 30.12% Submissions: 100k+ Points: 2
Given two numbers represented by two linked lists of size N and M. The task is to return a sum list.
The sum list is a linked list representation of the addition of two input numbers from the last.
Example 1:
Input:
N = 2
valueN[] = {4,5}
M = 3
valueM[] = {3,4,5}
Output: 3 9 0
Explanation: For the given two linked
list (4 5) and (3 4 5), after adding
the two linked list resultant linked
list will be (3 9 0).
*/
Node*reverse(Node *h){
Node *r=NULL;
Node* p=h;
Node* q=NULL;
while(p){
r=q;
q=p;
p=p->next;
q->next=r;
}
return q;
}
//Function to add two numbers represented by linked list.
struct Node* addTwoLists(struct Node* first, struct Node* second)
{
Node*l1=reverse(first);
Node*l2=reverse(second);
Node*dummy=new Node(0);
Node*temp=dummy;
int c=0;
while(l1!=NULL || l2!=NULL ||c){
int sum=0;
if(l1!=NULL){
sum+=l1->data;
l1=l1->next;
}
if(l2!=NULL){
sum+=l2->data;
l2=l2->next;
}
sum+=c;
c=sum/10;
Node*sumi=new Node(sum%10);
temp->next=sumi;
temp=temp->next;
}
Node*ans=reverse(dummy->next);
return ans;
}
/*
Linked List in Zig-Zag fashion
Easy Accuracy: 51.51% Submissions: 11145 Points: 2
Given a Linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f ..
where a, b, c are consecutive data node of linked list and such that
the order of linked list is sustained.
For example: In 11 15 20 5 10 we consider only 11 20 5 15 10 because it satisfies the above condition and
the order of linked list. 5 20 11 15 10 is not considered as it does not follow the order of linked list.
To maintain the order, keep swapping the adjacent nodes of the linked list (whenever required) to get the desired output.
Example 1:
Input:
LinkedList: 1->2->3->4
Output: 1 3 2 4
*/
void swap(Node *a,Node *b){
int t = a->data;
a->data = b->data;
b->data = t;
}
Node *zigZag(Node* head)
{
// your code goes here
Node *temp = head;
int bit = 0;
while(temp->next){
if(bit){
if(temp->data < temp->next->data)
swap(temp,temp->next);
bit = 0;
}
else{
if(temp->data > temp->next->data) swap(temp,temp->next);
bit = 1;
}
temp = temp->next;
}
return head;
}
//QUICK SORT LINKED LIST
struct node*pivot(struct node*low,struct node*high){
struct node* pvt = low, *curr = low->next, *prev= low;
while(curr!=high->next){
if(pvt->data>curr->data){
swap(prev->next->data,curr->data);
prev=prev->next;
}
curr=curr->next;
}
swap(prev->data,pvt->data);
return prev;
}
void solve(node*low,node*high){
if(!low || !high ||low==high){
return;
}
node*pvt=pivot(low,high);
solve(low,pvt);
solve(pvt->next,high);
}
void quickSort(struct node **head) {
if(!(*head)|| !(*head)->next)return ;
struct node*high=*head;
while(high->next)high=high->next;
solve(*head,high);
}
/*
Intersection Point in Y Shapped Linked Lists
Medium Accuracy: 49.55% Submissions: 100k+ Points: 4
Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.
example 1:
Input:
LinkList1 = 3->6->9->common
LinkList2 = 10->common
common = 15->30->NULL
Output: 15
Explanation:
Y ShapedLinked List
*/
int intersectPoint(Node* head1, Node* head2)
{
int length1=0;
int length2=0;
Node*p=head1;
Node*q=head2;
while(p!=NULL){
length1++;
p=p->next;
}
while(q!=NULL){
length2++;
q=q->next;
}
int res=abs(length1-length2);
if(length1>length2){
p=head1;
q=head2;
}
else{
p=head2;
q=head1;
}
for(int i=0;i<res;i++){
p=p->next;
}
while(p!=NULL && q!=NULL){
if(p==q)return p->data;
else{
p=p->next;
q=q->next;
}
}
return -1;
}
/*
Rotate a Linked List
Medium Accuracy: 33.33% Submissions: 100k+ Points: 4
Given a singly linked list of size N.
The task is to left-shift the linked list by k nodes, where k is a given positive integer smaller than or equal to length of the linked list.
Example 1:
Input:
N = 5
value[] = {2, 4, 7, 8, 9}
k = 3
Output: 8 9 2 4 7
Explanation:
Rotate 1: 4 -> 7 -> 8 -> 9 -> 2
Rotate 2: 7 -> 8 -> 9 -> 2 -> 4
Rotate 3: 8 -> 9 -> 2 -> 4 -> 7
*/
Node* rotate(Node* head, int k)
{
Node*ptr=head;
if(head==NULL|| head->next==NULL)
return head;
for(int i=1;i<k;i++){
ptr=ptr->next;
}
Node*curr=ptr;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=head;
head=curr->next;
curr->next=NULL;
return head;
// Your code here
}
/*
Remove loop in Linked List
Medium Accuracy: 47.96% Submissions: 100k+ Points: 4
Given a linked list of N nodes such that it may contain a loop.
A loop here means that the last node of the link list is connected to the node at position X. If the link list does not have any loop, X=0.
Remove the loop from the linked list, if it is present.
Example 1:
Input:
N = 3
value[] = {1,3,4}
X = 2
Output: 1
Explanation: The link list looks like
1 -> 3 -> 4
^ |
|____|
A loop is present. If you remove it
successfully, the answer will be 1.
*/
void removeLoop(Node* head)
{
Node*slow=head;
Node*fast=head;
while(slow && fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast)break;
}
if(slow==head){
while(fast->next!=head){
fast=fast->next;
}
fast->next=NULL;
}
else if(slow==fast){
slow=head;
while(slow->next!=fast->next){
slow=slow->next;
fast=fast->next;
}
fast->next=NULL;
}
/*
Reverse alternate nodes in Link List
Medium Accuracy: 50.78% Submissions: 11632 Points: 4
Given a linked list, you have to perform the following task:
Extract the alternative nodes starting from second node.
Reverse the extracted list.
Append the extracted list at the end of the original list.
​Example 1:
Input:
LinkedList = 10->4->9->1->3->5->9->4
Output: 10 9 3 9 4 5 1 4
Explanation: Reversing the alternative
nodes from the given list, and then
appending them to the end of the list
results in a list with
*/
void rearrange(struct Node *odd)
{
struct Node *t=odd,*m=t->next,*r=t->next;
while(t && t->next && t->next->next!=NULL)
{
t->next=t->next->next;
t=t->next;
m->next=t->next;
m=t->next;
}
t->next=NULL;
m=r;
struct Node *next,*prev=NULL;
while(m!=NULL)
{
next=m->next;
m->next=prev;
prev=m;
m=next;
}
t->next=prev;
}
/*
Flattening a Linked List
Medium Accuracy: 33.91% Submissions: 69318 Points: 4
Given a Linked List of size N, where every node represents a sub-linked-list and contains two pointers:
(i) a next pointer to the next node,
(ii) a bottom pointer to a linked list where this node is head.
Each of the sub-linked-list is in sorted order.
Flatten the Link List such that all the nodes appear in a single level while maintaining the sorted order.
Note: The flattened list will be printed using the bottom pointer instead of next pointer.
Example 1:
Input:
5 -> 10 -> 19 -> 28
| | | |
7 20 22 35
| | |
8 50 40
| |
30 45
Output: 5-> 7-> 8- > 10 -> 19-> 20->
22-> 28-> 30-> 35-> 40-> 45-> 50.
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
*/
Node* merge(Node* a,Node*b)
{
if(a==NULL)
return b;
if(b==NULL)
return a;
if(a->data<b->data)
{
a->bottom=merge(a->bottom,b);
return a;
}
else
{
b->bottom=merge(a,b->bottom);
return b;
}
}
Node *flatten(Node *root)
{
if(root==NULL|| root->next==NULL)
return root;
return merge(root,flatten(root->next));
// Your code here
}
/*
Insertion Sort for Singly Linked List
*/
// Your code
Node* insertionSort(struct Node* head)
{
//code here