No video

Detect and Remove Loop in a Linked List | GeeksforGeeks

  Рет қаралды 75,462

GeeksforGeeks

GeeksforGeeks

7 жыл бұрын

Find Complete Code at GeeksforGeeks Article: www.geeksforgeeks.org/detect-a...
Practice Problem Online Judge: practice.geeksforgeeks.org/pro...
Soundtrack: Aretes by Kevin MacLeod
Read More: www.geeksforgeeks.org/detect-...
This video is contributed by Ishant Periwal
Please Like, Comment and Share the Video among your friends.
Also, Subscribe if you haven't already! :)

Пікірлер: 69
@p33yush
@p33yush 4 жыл бұрын
I use this tutorial as background music while studying. Amazing tutorial thanks G4G!
@LuciferFayte
@LuciferFayte 6 жыл бұрын
Hi, this is a great video, but there's just one thing I'd like to point out that might help others: this solution doesn't seem to deal with the removal of a full cycle correctly. when I was testing it out myself I created this loop: [1]->[2]->[3]->[4]->[5]->[6]->[7]->[1]->[2]->...etc. the cycle was detected and corrected the linked list to this: [1] due to the fast and slow nodes both being at the root/head when the cycle was detected; moving slow back to root/head meant the while loop checked it's condition, ended, and caused fast->next (which would be node "[2]") to get turned into nullptr, making the list just [1] instead of [1]->[2]->[3]->[4]->[5]->[6]->[7]. had to add an extra conditional to see if both fast and slow were on the root node, and loop the fast pointer around until fast->next was equivalent to the root/head node, and then set fast->next to nullptr. though the solution is simple enough, it would have been nice to have it included :)
@AayushSoni1196
@AayushSoni1196 5 жыл бұрын
Another way to go around this would be to initialize slow and fast to different nodes(1st and 2nd for example) at the beginning. That way the cycle will still be detected but the loop_node will never be head, but now the nodes must be counted(method 2) to break the cycle.
@joydeb8202
@joydeb8202 3 жыл бұрын
The person who made this tutorial needs a HatsOff... Thanks GFG for this.
@abhishek-bl6re
@abhishek-bl6re 4 жыл бұрын
Geeks For Geeks is the best Platform to master Data Structures As they have the best quality and logical questions and very thoughful.Big Thanks!
@Msiva-gv8lm
@Msiva-gv8lm Жыл бұрын
Simply superb, no wasting of time and easy understanding ❤
@parthmakkar3116
@parthmakkar3116 4 жыл бұрын
1-2-3-1 .... last method is not working on this test case , in this slow and fast pointer is coming on head pointer which is "1" while detecting loop , ... can anyone help me out ??
@saurabhkacholiya
@saurabhkacholiya 6 жыл бұрын
awesome videos best way to learn interview question and best way to learn DS
@masumali8356
@masumali8356 5 ай бұрын
thanks for your hard work for better understand.
@tnduc91
@tnduc91 6 жыл бұрын
so easy to understand, big thanks!
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
You're welcome Tran Duc!
@badrinarayana9932
@badrinarayana9932 7 жыл бұрын
which software was used to do the animation?? Superb animation
@JustBansuri
@JustBansuri 5 жыл бұрын
powerpoint.
@angitd3766
@angitd3766 7 жыл бұрын
Best video so far
@AshutoshSingh-qj1gm
@AshutoshSingh-qj1gm 3 жыл бұрын
great, I had one more approach although it uses extra memory. While traversing, keep pushing the nodes in map. If anytime we get the same node again, we will find out that instance, not only we will be able to detect the loop, but also the node where loop happens.
@rithiksingh1395
@rithiksingh1395 5 жыл бұрын
superb!! fabulous
@sukritkapil2562
@sukritkapil2562 4 жыл бұрын
nice explanation!
@soumavanag5025
@soumavanag5025 7 жыл бұрын
Simply superb :)
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
Thanks Soumava Nag :)
@AnandKumar-qs3ed
@AnandKumar-qs3ed 5 жыл бұрын
In the second method, we can remove extra for loop, as node *removeloop1(node *root, node *loop) { node *ptr1,*ptr2; ptr1=loop; / /meeting point of the fast and slow pointer meet. ptr2=root; //head while(ptr1->next!=loop){ ptr1=ptr1->next; ptr2=ptr2->next; } ptr1=root; while(ptr2->next!=ptr1){ ptr2=ptr2->next; ptr1=ptr1->next; } ptr2->next=NULL; return root; }
@huh_wtf
@huh_wtf 4 жыл бұрын
I doubt if method 3 works...I don't understand...what if the head is at 2 or the linked list had another head say 0... wouldn't it make it loop infinitely?
@shroudyboi5673
@shroudyboi5673 4 жыл бұрын
wow this is amazing
@Vineethchowdhary
@Vineethchowdhary 6 жыл бұрын
nice info, thanks. I have a question why traversing at 2 for ptr2 ? why cant be 3X ?
@jieweiornoway
@jieweiornoway 5 жыл бұрын
cannot, the equation doesn't work for 3x.
@navinilan6993
@navinilan6993 4 жыл бұрын
Can find the loop....but loop removal can't use the formula...
@matitiudeforever8155
@matitiudeforever8155 6 жыл бұрын
nice explaination but i think in detection of loop ....... in while loop statement writing "while fast.next and fast" would be sufficient......correct me if i am wrong
@AsifIqbalR
@AsifIqbalR 5 жыл бұрын
you are wrong
@divyamsh2115
@divyamsh2115 5 жыл бұрын
That will work!
@Shishiraithal
@Shishiraithal 4 жыл бұрын
Thankyou..🥰🥰😍😍😍
@angitd3766
@angitd3766 7 жыл бұрын
In the 2nd method what if there are some 10 or 20 nodes before the head node of the loop(in this case 3th node) with the size of the loop still 6.For eg,the list is 0-10-11-1-2-12-13-14-15-16-17-(3-4-5-6-7-8) where 3 till 8 forms the loop.The ptr2 after the for loop it will be pointing somewhere in the linear part of the list which cannot move in cycle.Plizz explain
@Grimlock1979
@Grimlock1979 7 жыл бұрын
ptr2 doesn't need to start inside the loop. In the last step, ptr1 will start at 0 and ptr2 will start at 13. Then, you move both of them until they meet. They will both move 11 steps (ptr2 will make one loop) until they meet at 3.
@jieweiornoway
@jieweiornoway 5 жыл бұрын
If it takes K steps for the slow runner to enter the loop, and the fast runner moves at double the pace as the slow runner. Then the fast runner is 2K - K step or K steps into the loop. 1) When the slowRunner is 0 step inside the loop. 2) The fastRunner is K steps into the loop. 3) slowRunner is K steps behind FastRunner. 4) FastRunner is Loop_size - K steps behind slowRunner. So because fastRunner catches up to the slowRunner at a pace of 1 per step. It will take Loop_size - K to catch up to it. so if you have a loop size of 8 and your fastRunner is 3 steps into the loop which = K. And if you traverse the difference (Loop_size-k) of 5 steps to get to where they collide then it means that there are 3 more steps or k steps to the head of the circle. Because K = mod(k, loop_size), this works for any size of loop. And since it took slowRunner k step to get inside of the loop, the collision spot and head of the linked list is K steps away from the beginning of the loop. Then you can move the slow pointer back to the beginning and move step by step to find the head of the loop.
@kaushaljhawar3282
@kaushaljhawar3282 7 жыл бұрын
Nicely explained
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
Thanks Kaushal!
@Shra1thedon
@Shra1thedon 6 жыл бұрын
Can someone tell me what is the use of finding k ?
@faizanmemon4956
@faizanmemon4956 5 жыл бұрын
You saved a headache !
@kremit3619
@kremit3619 5 жыл бұрын
In the second method, why is it that ptr2 will only terminate once it can reach ptr1? Why can't we check for ptr2->next == loop_node and make the cut there to cut off the loop? Thanks.
@jieweiornoway
@jieweiornoway 5 жыл бұрын
Because the equation works for all size of loop. You don't really know where the start of the loop is at that point. You need to find it. Basically, if it takes K steps for the slow runner to enter the loop, and the fast runner moves at double the pace as the slow runner. Then the fast runner is 2K - K step or K steps into the loop. 1) When the slowRunner is 0 step inside the loop. 2) The fastRunner is K steps into the loop. 3) slowRunner is K steps behind FastRunner. 4) FastRunner is Loop_size - K steps behind slowRunner. So because fastRunner catches up to the slowRunner at a pace of 1 per step. It will take Loop_size - K to catch up to it. so if you have a loop size of 8 and your fastRunner is 3 steps into the loop which = K. And if you traverse the difference (Loop_size-k) of 5 steps to get to where they collide then it means that there are 3 more steps or k steps to the head of the circle. Because K = mod(k, loop_size), this works for any size of loop. And since it took slowRunner k step to get inside of the loop, the collision spot and head of the linked list is K steps away from the beginning of the loop. Then you can move the slow pointer back to the beginning and move step by step to find the head of the loop.
@gopikumarkaushik8828
@gopikumarkaushik8828 3 жыл бұрын
best way to teach ds
@mshahzeb8926
@mshahzeb8926 3 жыл бұрын
AMAZING !
@dharmaputra7394
@dharmaputra7394 6 жыл бұрын
loop same with cycle?
@Gregg_the_egg
@Gregg_the_egg 4 жыл бұрын
Hi All, I didn't quite understand the fact that if k+m is a multiple of n then how the fast pointer and the slow pointer will meet at the starting point of the loop? Can Anyone help me understand it?
@sanskritikalra5751
@sanskritikalra5751 4 жыл бұрын
Let us say that n=k+b.---(1) and it has already been proved that k+m is a multiple of n. =>(k+m)=a*n---(2) Subtracting two equations, =>m-b=(a-1)*n =>m= b+(a-1)*n Hence it suggests that m is the distance equal to certain number of full iterations in the circle and distance b. hence, they definitely meet.
@manishpritwani
@manishpritwani 2 жыл бұрын
(k+m) is multiple of n, so k+m=a*n where a is that multiple shifting k to right m=(a*n) - k for simplicity let's see (a*n) as 'n' only m=n-k This (n-k) is the distance between meeting point(k) and start of the cycle .(refer diagram) From this point one went to head. So if ptr1 starts from head and covers 'm' distance, then it's bound to meet ptr2 which starts from that meeting point(k) and reaches start point of cycle.
@rahulkhandelwal8781
@rahulkhandelwal8781 7 жыл бұрын
One of the best video!
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
Thanks Rahul!
@aniketyadav6009
@aniketyadav6009 3 жыл бұрын
Last method does not work for multiple test cases
@kashishmittal566
@kashishmittal566 6 жыл бұрын
AWESOME.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
Thanks kashish mittal :)
@coolshailendra2805
@coolshailendra2805 6 жыл бұрын
how 2nd method is solving problem where there are more nodes than k count before loop. like. 2->3->4->5->6->7->9->10->11->12->13->14->11 . 4 nodes in loop. so ptr1 and ptr2 both will start from head and after k(4) ptr2 will be at 6. I did not get how ptr1 and ptr2 are always going to meet on loop start node. I mean i din get math here. Might be sue to logic explain in 3rd. but nice video with superb animation.
@jieweiornoway
@jieweiornoway 5 жыл бұрын
I came here looking for better explanation to this solution but didn't find it. Let me try to explain it to you and maybe I will get it in the process as well. If it takes K steps for the slow runner to enter the loop, and the fast runner moves at double the pace as the slow runner. Then the fast runner is 2K - K step or K steps into the loop. 1) When the slowRunner is 0 step inside the loop. 2) The fastRunner is K steps into the loop. 3) slowRunner is K steps behind FastRunner. 4) FastRunner is Loop_size - K steps behind slowRunner. So because fastRunner catches up to the slowRunner at a pace of 1 per step. It will take Loop_size - K to catch up to it. so if you have a loop size of 8 and your fastRunner is 3 steps into the loop which = K. And if you traverse the difference (Loop_size-k) of 5 steps to get to where they collide then it means that there are 3 more steps or k steps to the head of the circle. Because K = mod(k, loop_size), this works for any size of loop. And since it took slowRunner k step to get inside of the loop, the collision spot and head of the linked list is K steps away from the beginning of the loop. Then you can move the slow pointer back to the beginning and move step by step to find the head of the loop. Wow explaining it to someone else really helped me get this. I hope it helped you too. Let me know if something confused you.
@abhishekpatil569
@abhishekpatil569 2 жыл бұрын
Can anyone help? whats going wrong here? void removeLoop(Node* head) { // code here // just remove the loop without losing any nodes Node *slow, *fast; slow = head; fast = head; slow = slow->next; fast = fast->next->next; while(fast && fast->next) { if(slow == fast) break; slow = slow->next; fast = fast->next->next; } if(slow == fast) { slow = head; while(slow->next != fast->next) { slow = slow->next; fast = fast->next; } fast->next = NULL; } } input:- 4 1 2 3 4 1 output:- 0 // not removed expected output:- 1 // removed
@palashshukla9703
@palashshukla9703 5 жыл бұрын
Better way of explanation .........
@programmertik2046
@programmertik2046 3 жыл бұрын
anyone the intuition behind the seconnd method ????
@dhruvgoyal766
@dhruvgoyal766 3 жыл бұрын
Kindly add voice over please
@ravikantkumar9698
@ravikantkumar9698 3 жыл бұрын
last method fails when i goes to submit code of last approach:
@manikanth.8507
@manikanth.8507 3 жыл бұрын
check for circular linked list condition. c++ code: if(slow==fast) { slow=head; if(slow == fast) { while(fast->next != slow) fast = fast->next; } else { while(slow->next!=fast->next) { slow=slow->next; fast=fast->next; } } fast->next=NULL; }
@nabhisheksingh1399
@nabhisheksingh1399 3 жыл бұрын
@@manikanth.8507 Thanks man, Trying to find a solution since two hours . Your comment came as miracle !!!
@asutoshmahapatro5903
@asutoshmahapatro5903 4 жыл бұрын
Nice graphics
@manavgoyal8485
@manavgoyal8485 5 жыл бұрын
Perspicuously Explained
@manikanth.8507
@manikanth.8507 3 жыл бұрын
In 3rd method we need some modification: we have to check condition that it is not forming complete cycle(i.e circular linked list).In case of circular linked list fast && slow are pointing to head that's why while loop is not working. c++ code: if(slow==fast) { slow=head; if(slow == fast) { while(fast->next != slow) fast = fast->next; } else { while(slow->next!=fast->next) { slow=slow->next; fast=fast->next; } } fast->next=NULL; }
@ytbyronak2543
@ytbyronak2543 2 жыл бұрын
This is linked list, so it will definitely be different from circular linked lists.
@adityaaggarwal4622
@adityaaggarwal4622 4 жыл бұрын
Bingo !
@DAaaMan64
@DAaaMan64 6 жыл бұрын
Such music.
@DAaaMan64
@DAaaMan64 6 жыл бұрын
wow.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
Thanks :)
@onkarsawant6291
@onkarsawant6291 3 жыл бұрын
Please share name of music
@surabhi6576
@surabhi6576 Жыл бұрын
wth is this backgroung music
Add two numbers represented by Linked Lists | Set 1 | GeeksforGeeks
4:33
Detect loop in linked list(floyd algo / Tortoise and hare algo)
9:29
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 111 М.
UNO!
00:18
БРУНО
Рет қаралды 3,4 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 12 МЛН
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 5 МЛН
Data Structures: Cycles in a Linked List
5:19
HackerRank
Рет қаралды 165 М.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 265 М.
LinkedList vs ArrayList in Java Tutorial - Which Should You Use?
11:43
Coding with John
Рет қаралды 582 М.
Linked lists in 4 minutes
4:22
Michael Sambol
Рет қаралды 66 М.
Reverse Linked List
5:58
Kevin Naughton Jr.
Рет қаралды 45 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 256 М.
Linked List Cycle II | LeetCode 142 | Medium
8:36
Code And Coffee
Рет қаралды 6 М.
Find the length of loop in linked list | GeeksforGeeks
4:04
GeeksforGeeks
Рет қаралды 16 М.
UNO!
00:18
БРУНО
Рет қаралды 3,4 МЛН