Detect loop in linked list(floyd algo / Tortoise and hare algo)

  Рет қаралды 112,207

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

Күн бұрын

Пікірлер: 142
@azeeztaiwo2802
@azeeztaiwo2802 4 жыл бұрын
I really like the way you explain the algorithm, so calm and straight to point
@leopoldroitel4022
@leopoldroitel4022 5 жыл бұрын
So basically unless you know Floyd's algorithm, you can't come this up with this by yourself.
@GKALYAN04
@GKALYAN04 4 жыл бұрын
Btw, You can use Hash Tables which has the same time complexity as Floyd's algo.
@the_chi
@the_chi 4 жыл бұрын
@@GKALYAN04 but not the same space complexity
@krisnadiputra93
@krisnadiputra93 4 жыл бұрын
lol, it s not true, at least Floyd could
@TonyDemetriou
@TonyDemetriou 4 жыл бұрын
You probably could come up with it yourself if you are thinking about it the right way. For me, if I think about a loop where I want to walk around it and stop on the same square I started on, then I would probably want to use the second pointer to stay still and when they're equal again we have reached the start. If you then said I have to move both pointers I might think of moving the second one at double speed so it will do two loops and be at the start when the first pointer reaches the start. After that, I can figure out how to start with a chain that becomes a cycle, but they wouldn't meet at the right square, and I'd think about how with the loop I started both pointers together, but with the chain one pointer has a headstart by the time the other starts on the loop. If the headstart is 2, then it will meet up 2 steps too early. That would tell me that the number of steps to the start of the loop is the same as the number of steps in the chain, but I don't know what that number is. But if we take the same number of steps along the chain and the remaining loop we will meet at the start of the loop. Then I would think about unusual situations, like when there's an even vs odd number of steps, or what if the chain is much longer than the loop, or the loop is much longer than the chain. ... So I'd probably get to the right answer, but ONLY because of the way I started thinking about it, and because of the questions getting asked. If I was just programming and wanted to loop around to the start, then I wouldn't need to move the second pointer, and I wouldn't be thinking about the rest and wouldn't figure out that you can detect cycles like this. Or if you just gave me a list of nodes and asked me to check if there are cycles, I'd probably start thinking about starting from the beginning of the chain rather than starting with the loop, and I personally probably would not figure it out. People trained in formal mathematics have ways of working through problems like this. I'm unfortunately not one of them - for me, a lot of the time it's whether I'm asking myself the right questions, and that is usually just luck depending on where my mind wanders at that particular moment.
@karana2260
@karana2260 4 жыл бұрын
if you jog with people in a park, or watch f1, you could come up with this solution.
@Artshi_Sakshi
@Artshi_Sakshi 3 жыл бұрын
sir ,the way you explain makes it so easy to understand......kudos to your effort!!!!
@lifekool5838
@lifekool5838 2 жыл бұрын
Such a simple yet powerful explanation of various algorithms. I hated algos... but now it is fun due to your videos and your positivity is icing on the cake. Thank you sir... for amazing videos.
@shashankparmar7899
@shashankparmar7899 4 жыл бұрын
Great solution sir!!! I have always used unordered map for this. (unordered_map map). But, this solution is very elegant and also requires only O(1) space complexity, unlike unordered_map solution.
@Cybernetic1
@Cybernetic1 5 жыл бұрын
Sir, I have big fan of yours..... i love the way you explain... the way you talk slow and simplicity in your face.
@mp0157
@mp0157 5 жыл бұрын
Vivekanand, You kept the explanation quite simple & straightforward! It greatly helped :) Thank you!
@shivamrajput3321
@shivamrajput3321 4 жыл бұрын
the way you explain is very easy to visualize and implement. even a guy like me who gets bamboozled seeing these algorithms is able to keep up. Nice going bro. Hoping for your prosperity
@nareshtanniru
@nareshtanniru 5 жыл бұрын
Thanks For video. To find start of loop here is another alternative after p and q meet. we will make q pointer stop at that point and make p pointer move till it reaches q pointer again and count number of nodes in loop. this will give us length of cycle. ( how many nodes are in cycle). lets denote dis with c. then just take 2 pointers p1 and p2 at c distance apart.. and move both at SAME speed till p1 and p2 meets. that point will be start of loop.
@zippy.gaurav
@zippy.gaurav 5 жыл бұрын
Thank you for posting your videos free of cost. I really am thankful to you.
@PaulFromMalta
@PaulFromMalta 4 жыл бұрын
REALLY AWESOME EXPLANATION!!! Thank you so much for taking the time to make this video. 9 mins and 28 seconds of no bullshit, and just fundamental basic concepts built on top of each other in a crystal clear way. Please keep making vids!!!!
@joemoe5954
@joemoe5954 7 жыл бұрын
Thank you! This helped me understand how to detect a loop within a link list quickly and in an understandable manner!
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Joseph...!
@adityapaithon6499
@adityapaithon6499 6 жыл бұрын
What's the time complexity of this approach
@vaibhavlaxman4959
@vaibhavlaxman4959 2 жыл бұрын
@@adityapaithon6499 O(n)
@fahim.m.choudhury
@fahim.m.choudhury 4 жыл бұрын
Thank you very much! Your explanation and teaching are very clear.
@gin8964
@gin8964 4 жыл бұрын
Very straightforward explanation. Thank you so much!
@chunkwanchan5503
@chunkwanchan5503 11 ай бұрын
Thanks! can I start p pointer at head & q pointer at head.next? I found in some cycle structure, there's infinite loop where 2 pointers will never meet?
@poojaupadhyay6364
@poojaupadhyay6364 4 жыл бұрын
very clear and concise explaination
@krisshore2917
@krisshore2917 5 жыл бұрын
Simplest explanation for this algorithm. Thank you.
@crazyvlog5830
@crazyvlog5830 3 жыл бұрын
🎂🎂🎂🎂🎉🎉🎉🎉🎉🎂
@RadhamaniRamadoss
@RadhamaniRamadoss 2 жыл бұрын
Loved the explanation ..Thankyou sir
@cppdog3549
@cppdog3549 2 жыл бұрын
To find the start of the cycle, skip to 6:00
@sudomoon
@sudomoon 4 жыл бұрын
Simply Wonderful Explanation
@varnanthirugnanasambandan559
@varnanthirugnanasambandan559 2 жыл бұрын
Very clear explanation 🙏
@nahidulislam9000
@nahidulislam9000 3 жыл бұрын
Your video is much more understandable than others.
@nouru6856
@nouru6856 6 жыл бұрын
@Vivekanand your the best!!!! Thumbs upppp
@gawarivivek
@gawarivivek 6 жыл бұрын
I'm loving your videos, man!! Clear explanation.. Thanks for sharing your knowledge with us.. Keep it up!!
@kewtomrao
@kewtomrao 4 жыл бұрын
Why the haters?So nicely explained......Keep it up!
@prajwalcr7799
@prajwalcr7799 3 жыл бұрын
How will be the linked list already present in our IDE as we are directly checking instead of inputting anything
@vinaynadig3258
@vinaynadig3258 7 жыл бұрын
simple but perfect explanations. Liked it
@jh9104
@jh9104 5 жыл бұрын
Super clear explanation of the concept. Thank you!
@mihirjoshi3589
@mihirjoshi3589 6 жыл бұрын
Your videos are very helpful. Could you please make one for LRU Cache? Superb initiative, thanks a lot!
@DarshanKumarTV-ur8tw
@DarshanKumarTV-ur8tw 10 ай бұрын
satisfactory teaching sir
@smritidubey5781
@smritidubey5781 3 жыл бұрын
Sir, why q->next!=NULL is considered in the while loop?
@bisatisrilatha7957
@bisatisrilatha7957 3 жыл бұрын
TQsm sir it was very helpful vedio.
@PriteshRanjan30
@PriteshRanjan30 6 жыл бұрын
you have very nicely explained this solution.please Provide dynamic programming video explanation with examples
@karthikeyaacharya8424
@karthikeyaacharya8424 4 жыл бұрын
Awesome and simple explanation !!
@angelroma5456
@angelroma5456 3 жыл бұрын
Thank you bro, pretty simple to understand!
@trestenpool9045
@trestenpool9045 3 жыл бұрын
Great explanation sir, thank you!
@sankaranarayananh7957
@sankaranarayananh7957 5 жыл бұрын
Awesome explanation sir! Thank you
@akshayroy6261
@akshayroy6261 4 жыл бұрын
Great Explanation 😀
@hemantmangwani7172
@hemantmangwani7172 4 жыл бұрын
Thank you sir . Its Helped a lot.
@AbhishekSharma-uh8pb
@AbhishekSharma-uh8pb 3 жыл бұрын
When you starting again sir . Very good 👍 explanation
@HarmeetKaur
@HarmeetKaur 6 жыл бұрын
Thank you for these videos. Really helpful!
@priyanshuyadav8173
@priyanshuyadav8173 3 жыл бұрын
You're a great teacher ✨✨
@Ankit13555
@Ankit13555 7 жыл бұрын
Can you please explain Brent's Cycle Detection Algorithm ..that will give an edge in interviews
@anshiknigam671
@anshiknigam671 7 жыл бұрын
while(p && q && q->next) why only we are checking q->next. should we also place p->next also. please clear the doubt, is it important to place q->next in this condition. I think that p && q is enough. if I am wrong please correct me, anyone.
@nands4410
@nands4410 6 жыл бұрын
Anshik Nigam q is ahead of p by one step so we need to check for q itself.
@pavankumard5276
@pavankumard5276 5 жыл бұрын
wont this show an error if there is no loop and the linked list consists of 4 nodes. because then p&&q&&q->next is not null but q->next->next=null it will show exception in java
@AjinkyaWasnik
@AjinkyaWasnik 5 жыл бұрын
Perfectly explained. Whatever questions aroused while watching the video were answered right then and there. :) keep posting videos, sir.
@jeevan999able
@jeevan999able 4 жыл бұрын
*arise
@lotusithok4985
@lotusithok4985 7 жыл бұрын
Best explaination so far...
@xof8256
@xof8256 5 жыл бұрын
Mast bhava, dhanyawad
@anirudhbharti3013
@anirudhbharti3013 4 жыл бұрын
I think for remove_loop while(p->Next!=q->Next) should be the condition
@vighneshk509
@vighneshk509 4 жыл бұрын
sir in while loop you said anyone of those condition but that is AND operator so all condition must be satisfied not just one of them
@simonwathigo604
@simonwathigo604 4 жыл бұрын
I think it was just a mistake. Having a nil value will raise an runtime error `cant access next attribute for 'nil' value`.
@ayushikumar34
@ayushikumar34 6 жыл бұрын
Thanks a lot for a great explanation .. keep up your good wrk ... you are really good
@sunnyjain630
@sunnyjain630 6 жыл бұрын
very nice expland should we have p=p->next and q=q->next->next in case of removal of loop detected by detection algo in step1 plz clear my doubt any one.... thanks in adv
@nands4410
@nands4410 6 жыл бұрын
I am no expert but I would maintain a previous pointer to p say "previous" and when we detect the start of loop i would set the previous-next->NULL. This would remove the loop.
@ayushikumar34
@ayushikumar34 6 жыл бұрын
yes true ..what is written on board is wrong ..probably coz of which he didnt explain ...it shud be q->next->next
@AddieInGermany
@AddieInGermany 7 жыл бұрын
Sir i cannot find Floyd Algo.. Can you please provide the link ?
@srinivasanshanmugam9527
@srinivasanshanmugam9527 6 жыл бұрын
Pls post the video for reversing the doubly linked lists
@kingdey9136
@kingdey9136 4 жыл бұрын
Thank you!
@chhamasahu5987
@chhamasahu5987 7 жыл бұрын
Hello ,sir can you give us the link for the explanation on why this algorithm work.I couldn't find it on your channel.
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
"Why floyd's cycle detection algorithm works? " on this channel. Please check. Thanks.
@ranjanasinha
@ranjanasinha 5 жыл бұрын
Loved it. Thanks a lot.
@hitec1691
@hitec1691 5 жыл бұрын
India!! :)
@santoshreddy9963
@santoshreddy9963 3 жыл бұрын
Sir slow pointer null condition check karne ki zaroor nhi he.
@kanakadasyankor
@kanakadasyankor 7 жыл бұрын
Hi Vivek .. Awesome..thanks a lot.. any videos on Graphs ?
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Kanakadas.......sure will soon start a series on Graphs...!
@rashmikiranpandit8962
@rashmikiranpandit8962 4 жыл бұрын
I hit subscribe cuz your innocent way of asking us to subscribe was so cutee..:)
@madhavikatta7346
@madhavikatta7346 4 жыл бұрын
Thank you sir
@arvindkumarsharma5536
@arvindkumarsharma5536 5 жыл бұрын
Well explained as always..
@ankurrohilla4655
@ankurrohilla4655 4 жыл бұрын
Sir,why u stop uploading videos!!!!!!!!! u r the best
@anandkulkarni2111
@anandkulkarni2111 7 жыл бұрын
10 /10 explanation kudos!!
@anilkumaryadavchimaldhari7695
@anilkumaryadavchimaldhari7695 3 жыл бұрын
Simply super
@karynayehorova606
@karynayehorova606 5 жыл бұрын
Awesome. Thank you.
@ShivamKendre-fc3su
@ShivamKendre-fc3su 4 жыл бұрын
Greay explanation!! keep it up
@mannankathuria8879
@mannankathuria8879 5 жыл бұрын
Count the number of nodes in a link list program please
@mainulhasan136
@mainulhasan136 4 жыл бұрын
You are too good..
@deepakborah8335
@deepakborah8335 6 жыл бұрын
Congratulations for making good video. Upload videos series on Merg,Quick and Radix sort algorithm and program.
@engineersride8273
@engineersride8273 7 жыл бұрын
Hello Vivekanand, please do upload a video on how to remove the loop from linked list
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
very sooon...! Thanks..!
@jeeveshrawal2576
@jeeveshrawal2576 7 жыл бұрын
Plz can u explain detection of loop in doubly linked list
@jaysahu357
@jaysahu357 7 жыл бұрын
Very nice sir
@chandralekha7717
@chandralekha7717 6 жыл бұрын
Great video thanks
@RahulKumar-qu1if
@RahulKumar-qu1if 4 жыл бұрын
Sir what is the time complexity of this prog.
@vishalvikram8637
@vishalvikram8637 4 жыл бұрын
O(n)
@Jarikraider
@Jarikraider 6 жыл бұрын
You probably want to ensure that q->next and q->next->next are not null in that order.
@mritunjaymishra5008
@mritunjaymishra5008 4 жыл бұрын
If there isn't a loop...then won't it run infinitely?🤔
@amanbajpai9513
@amanbajpai9513 3 жыл бұрын
it will null terminate
@fares.abuali
@fares.abuali 2 жыл бұрын
Thanks ❤
@gunjankumar7970
@gunjankumar7970 7 жыл бұрын
sir plze upload some important programming interview questions of binary search tree ....it's being difficult to understand due to recursion
@anuragsahoo1235
@anuragsahoo1235 3 жыл бұрын
let just say there is cycle between two node then it is better to check first then iterate.
@anilbharath1994
@anilbharath1994 7 жыл бұрын
It's really helped me a lot
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Thanks Anil...!
@ill-fatedstranger447
@ill-fatedstranger447 6 жыл бұрын
Many many thanks
@sudhak8569
@sudhak8569 5 жыл бұрын
Sir i cannot find full code in github...Can you please provide the link ?
@induroopa627
@induroopa627 6 жыл бұрын
Thanks a ton 👍🏻😍
@srinivasanshanmugam9527
@srinivasanshanmugam9527 6 жыл бұрын
Also please post detecting loop in doubly linked list
@carlosalba9690
@carlosalba9690 5 жыл бұрын
This amazing!!!!!!
@musicmania4609
@musicmania4609 6 жыл бұрын
Good job
@vishnuvardhan-md1ux
@vishnuvardhan-md1ux 7 жыл бұрын
bro please post more videos thank you for your videos and the help
@payalsagar1808
@payalsagar1808 4 жыл бұрын
great🙌
@uvenkatasaiswaroop2425
@uvenkatasaiswaroop2425 5 жыл бұрын
Boss , You r Awesome !!!
@Kaushikvel
@Kaushikvel 7 жыл бұрын
If you don't understand from this video, you will never understand from anywhere else.
@mayank.ranjan
@mayank.ranjan 6 жыл бұрын
true
@adityapaithon6499
@adityapaithon6499 6 жыл бұрын
What's the time complexity of this btw
@pymondo1147
@pymondo1147 6 жыл бұрын
hahaha absolutely true.. No one can give this much explanation. If people are still finding it difficult then they should find other jobs:-p
@aakash.nagpal98
@aakash.nagpal98 5 жыл бұрын
land le
@aarushiahuja3875
@aarushiahuja3875 6 жыл бұрын
Sir kindly explain the program of this topic you have provided on github , thank you in advance :)
@ankitvarmait
@ankitvarmait 6 жыл бұрын
Thanks Sir
@dhritisinha8377
@dhritisinha8377 4 жыл бұрын
Narsimha karumanchi ki book ka exact code chapa h
@AddieInGermany
@AddieInGermany 7 жыл бұрын
Sir where is the next video, i can't find it.
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
Hey I am sorry Aditya...I forgot to upload the video...I will upload the video soon...!
@AddieInGermany
@AddieInGermany 7 жыл бұрын
No problem sir. I found the solution on geeksforgeeks.
@MrPraveenBasavaraj
@MrPraveenBasavaraj 7 жыл бұрын
where is ur next video. can't find
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
"Why floyd's cycle detection algorithm works? " on this channel. Please check. Thanks.
@graingert
@graingert 6 жыл бұрын
"I will execute the code parallely": *executes code sequentially*
@salonirathi1158
@salonirathi1158 6 жыл бұрын
Awesome video :))
@ratmgant
@ratmgant 3 жыл бұрын
goat
Why Floyd's cycle detection algorithm works? Detecting loop in a linked list.
24:11
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 113 М.
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 77 МЛН
Всё пошло не по плану 😮
00:36
Miracle
Рет қаралды 3,4 МЛН
Good teacher wows kids with practical examples #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 13 МЛН
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34
L14. Detect a loop or cycle in LinkedList | With proof and Intuition
20:26
Check if the singly Linked List is a Palindrome
11:10
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 78 М.
Insert / add  a node in Doubly Linked List
9:20
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 125 М.
Find middle element in a Linked List | GeeksforGeeks
9:18
GeeksforGeeks
Рет қаралды 27 М.
How to find start of a loop in a Singly Linked List? (Animation)
12:45
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 421 М.
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 77 МЛН