Detect a Cycle in Linked List | Amazon | Samsung | Microsoft

preview_player
Показать описание

In case you are thinking to buy courses, please check below:

#dsa #leetcode #placements
Рекомендации по теме
Комментарии
Автор

Intuition- let's say 2 people are running in a circular track, one person is running slowly and another person is running faster(2 times the speed of first person)

After a certain period of time person 2 again meet or overtake person 1,

In that case we can conclude that the track is circular ( replace running track with our Linked list)

shiva_krishna_das
Автор

Whenever I need a clarity of a code this is the only platform which gives me transaprency in one shot! Keep it up!!

priyankagorkhe
Автор

Loved when striverr comes to the part of explaing the intuition behind the logic 🥰

siddharth.chandani
Автор

Bruh never stop this series ❤️❤️❤️❤️❤️

jaswanthkosanam
Автор

case 1 if there is no cycle

f reach null and loop will break and it will return false

case 2 if there is cycle

obviously f will come behind s
now f is moving at speed 2x and s is moving with speed x in same direction then we can say
with help of relative speed concept f is moving at speed x and s is at rest wrt f
hence f will surely catch s

f= fast
s= slow

(Physics relative velocity concept involved if you dont know about it ignore this explaination)

farziidentification
Автор

This series is helping a lot...thanks man

ajeetworking
Автор

understood, thanks for the great explanation

kaichang
Автор

UNDERSTOOD... !!!
Thanks, striver for the video... :)

ranasauravsingh
Автор

Thanks bro. Please don't stop this series, please. Please.

om__
Автор

Thanks a bunch vaeya... Literally u r a ray of hope for children who are not from iits

aishwarya
Автор

The two pointer algorithm is called FLYOD'S LOOP DETECTION .



Wud be great if u wud upload the detect and REMOVE loop part too

anuragtiwari
Автор

Thanks bro. Please don't stop this series, please.

rahulgovindkumar
Автор

Good Explaination✌❤
This problem can be modified a little if we want to find the starting point of the loop.

shashankojha
Автор

while (head != nullptr) {
if (head -> val == 1e5 + 1) return true;
else {
head -> val = 1e5 + 1;
head = head -> next;
}
}
return false;
this has better runtime but assuming u are allowed to write the node values

prathamsagar
Автор

Isn't the edge case condition "if (head.next == null) return false" wrong, because a Linked List can only have one node and still have cycle, that one node pointing to itself.

samyak
Автор

why are we using two conditions in the while loop ?? if fast->next==NULL then fast->next->next will also be NULL only

sowdub
Автор

Thanks for the video bro good explanation
Keep doing!

savalalingeshreddy
Автор

I HAVE GOT ONE MORE APPROACH (OPTIMAL APPROACH)->
FIRST OF ALL ASK THE RANGE OF NUMBERS TO THE INTERVEIWER
THEN PICK A NUMBER OTHER THAN THE RANGE SPECIAIFIED
THEN GO ON TRAVERSING THE LIST FROM THE START AND EVERYTIME YOU TRAVERSE THE LIST IN THE
NODE VALUE PART PUT THAT NUMBER
-> AT ANY POINT WHEN YOU ARE TRAVERSING THE LIST YOU FIND THAT NODE->VAL == THAT NO THEN RETURN TRUE
ELSE RETURN FALSE;.
WILL THIS WORK
TIME -> O(N), Space-O(1)

atharvakulkarni
Автор

Can't we use any Collection class here e.g ArrayList ? Why we have to use HashTable ? In List also we can check if Node exists or not within the collection

RahulKumar-edbt
Автор

It would be very helpful if you could upload head of the loop and remove cycle in a linkedlists Btw awesome explanation as always.

bloke