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

  Рет қаралды 132,919

take U forward

take U forward

Күн бұрын

Пікірлер: 125
@takeUforward
@takeUforward 4 жыл бұрын
Understooooooooooooooood? . Instagram(connect to know closely about updates): instagram.com/striver_79/ . . If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
@learnwithme7750
@learnwithme7750 4 жыл бұрын
if there is a cycle in it , then it always include last node?
@yatinarora1252
@yatinarora1252 3 жыл бұрын
@@learnwithme7750hey it depends upon consider if only 1 number and then it may have a cycle also if therre given 1,2,3,4,5 in this case it is said that 2to 3 node value has cycle it is not last node.Use more and more test cases.
@ankushrai3155
@ankushrai3155 3 жыл бұрын
These videos are so helpful. I have been trying to learn this stuff for a year now. and your videos just click with me. Keep doing the good work sir.
@infinityzero2321
@infinityzero2321 Жыл бұрын
Node* fast = head; Node* slow = head; while (fast != slow) { if (fast == NULL || fast->next == NULL || fast->next->next == NULL || slow->next == NULL) return false; fast = fast->next->next; slow = slow->next; } return true; why does this algo not work? tried asking chatgpt but in vain added the fast->next->next and slow_.next to check if that is the issue nut no... i dont understand why is it failing
@infinityzero2321
@infinityzero2321 Жыл бұрын
got the mistake 🤣🤣 as soon as i commented realised that I should use do while
@shiva_krishna_das
@shiva_krishna_das 3 жыл бұрын
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)
@mukundnayak7310
@mukundnayak7310 3 жыл бұрын
Thank you!
@yashlakade179
@yashlakade179 3 жыл бұрын
Thanks a lot for these.......!!!
@altafmazhar7762
@altafmazhar7762 3 жыл бұрын
Thanks buddy its helpful
@swapnilparashare2670
@swapnilparashare2670 2 жыл бұрын
Thank You for clear and simple explanation.
@pranav288
@pranav288 2 жыл бұрын
what do yall get by copying from leetcode
@priyankagorkhe2870
@priyankagorkhe2870 Жыл бұрын
Whenever I need a clarity of a code this is the only platform which gives me transaprency in one shot! Keep it up!!
@jaswanthkosanam9481
@jaswanthkosanam9481 4 жыл бұрын
Bruh never stop this series ❤️❤️❤️❤️❤️
@siddharth.chandani
@siddharth.chandani Жыл бұрын
Loved when striverr comes to the part of explaing the intuition behind the logic 🥰
@ajeetworking
@ajeetworking 4 жыл бұрын
This series is helping a lot...thanks man
@aishwarya1895
@aishwarya1895 4 жыл бұрын
Thanks a bunch vaeya... Literally u r a ray of hope for children who are not from iits
@takeUforward
@takeUforward 4 жыл бұрын
Thanks for commenting in every video and watching.
@sriramkrishnamurthy4473
@sriramkrishnamurthy4473 3 жыл бұрын
Bhai vaeya kya hota hai yar ?
@rajkamalingle9144
@rajkamalingle9144 2 жыл бұрын
@@sriramkrishnamurthy4473 bhaiya bolna chahti thi shayad
@om_1_2
@om_1_2 4 жыл бұрын
Thanks bro. Please don't stop this series, please. Please.
@shashankojha3452
@shashankojha3452 4 жыл бұрын
Good Explaination✌❤ This problem can be modified a little if we want to find the starting point of the loop.
@takeUforward
@takeUforward 4 жыл бұрын
Its in the sde sheet so expect it to be out soon
@rahulgovindkumar3105
@rahulgovindkumar3105 3 жыл бұрын
Thanks bro. Please don't stop this series, please. Please.Pleaseeeeeee
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... !!! Thanks, striver for the video... :)
@kaichang8186
@kaichang8186 3 ай бұрын
understood, thanks for the great explanation
@anuragtiwari2098
@anuragtiwari2098 4 жыл бұрын
The two pointer algorithm is called FLYOD'S LOOP DETECTION . Wud be great if u wud upload the detect and REMOVE loop part too
@takeUforward
@takeUforward 4 жыл бұрын
Coming soon
@bloke3253
@bloke3253 4 жыл бұрын
It would be very helpful if you could upload head of the loop and remove cycle in a linkedlists Btw awesome explanation as always.
@takeUforward
@takeUforward 4 жыл бұрын
Coming soon
@farziidentification8672
@farziidentification8672 Жыл бұрын
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)
@deepesh16b
@deepesh16b 2 жыл бұрын
Very good approach 🔥🔥🙏
@savalalingeshreddy6750
@savalalingeshreddy6750 4 жыл бұрын
Thanks for the video bro good explanation Keep doing!
@samyak1409
@samyak1409 2 жыл бұрын
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.
@GurankitSinghBehal
@GurankitSinghBehal 2 жыл бұрын
You just cleared a massive doubt bro
@samyak1409
@samyak1409 2 жыл бұрын
@@GurankitSinghBehal Hey bro, I just got your reply and read my comment, I was wrong, `head.next == null` IMPLIES that next of head is not pointing to anything. So, returning False is correct. - Samyak with improved DSA. PS: I wonder if I was drunk when I wrote the previous comment. 🤦
@GurankitSinghBehal
@GurankitSinghBehal 2 жыл бұрын
@@samyak1409 You actually wanted to write...... "fast.next==null"..... I understood it....... And now my question is solved
@samyak1409
@samyak1409 2 жыл бұрын
@@GurankitSinghBehal oh okay!
@GurankitSinghBehal
@GurankitSinghBehal 2 жыл бұрын
@@samyak1409 yes..... This logic was quite new to me "A single node can point to itself"..... And it can be called a loop in linked list
@prateekshourya
@prateekshourya 4 жыл бұрын
Please don't join unacademy 🙏
@abhimanyu6534
@abhimanyu6534 3 жыл бұрын
Great explanation always 💯💯💥🙏
@SANCHITJAIN_
@SANCHITJAIN_ 3 жыл бұрын
great explanation bhaiya..loved it :)
@dreamyme543
@dreamyme543 2 жыл бұрын
Great explanation as always. Thank you:)
@nileshsinha7869
@nileshsinha7869 4 жыл бұрын
Awesome👍.. please make day 6 question no. 3 video next 🙏
@takeUforward
@takeUforward 4 жыл бұрын
Coming soon
@rohitudasi1537
@rohitudasi1537 2 жыл бұрын
can we apply recursion and change the value temporary before dfs call and while in dfs if we found temporary value of any node we return true indicating the cycle and after coming from dfs call we again change the value of node to its original value .
@rohitudasi1537
@rohitudasi1537 2 жыл бұрын
//global variable static Node dummy = new Node(-100); public static boolean dfs(Node node){ //if node reached to null we will return false indicating that there is no loop if(node==null){ return false; } // if node reached to the dummy node this means there is a loop if(node == dummy){ return true; } //other wise store node's next in next variable Node next = node.next; //and point node's next to dummy node node.next = dummy; // store the ans in variable but return it after restructuring the link boolean ans = dfs(next); //restructuring the link to original next node.next = next; return ans; }
@prathamsagar5353
@prathamsagar5353 Жыл бұрын
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
@sowdub9445
@sowdub9445 Жыл бұрын
why are we using two conditions in the while loop ?? if fast->next==NULL then fast->next->next will also be NULL only
@ganavin3423
@ganavin3423 2 жыл бұрын
why can't we use only fast.next.next !=null instead of two condition in while loop
@atharvakulkarni2024
@atharvakulkarni2024 3 жыл бұрын
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 FINE????? TIME -> O(N) , Space-O(1)
@abusalin1933
@abusalin1933 Жыл бұрын
not O(1) space complexity I think bro if u track numbers from new space where u stored the numbers
@narsimhareddy4742
@narsimhareddy4742 Жыл бұрын
It works check the below code bool hasCycle(ListNode *head) { int changeNum = 1e6; ListNode *temp = head; while(temp!=NULL){ if(temp->val == 1e6){ return true; } else{ temp->val = 1e6; } temp = temp->next; } return false; }
@loserfruit9663
@loserfruit9663 3 жыл бұрын
Loved the intuition
@RahulKumar-ed1bt
@RahulKumar-ed1bt 2 жыл бұрын
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
@ajay.sinhmar
@ajay.sinhmar 4 жыл бұрын
We can just directly change the value of every visited node to anything like 10^7 (as every node value in question must be between |10^5| ) and if we found that value again, there's a loop. Do these kind of solution give a bad impression in interview??
@takeUforward
@takeUforward 4 жыл бұрын
Yes, altering of given list is a bad habit, don’t do that. Think in terms pf company work, will u alter a real time data any day like this to find out ans? Try to rekate
@lakshmiprasanna7058
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@aseemsharma4643
@aseemsharma4643 2 жыл бұрын
Why do we increment fast pointer with 2 steps, if we increment it by 3 or 4 step we will reach the cycle faster right? Why 2 step only then?
@ashwanisingh4931
@ashwanisingh4931 4 жыл бұрын
Awesome explanation 👌
@apollodavis4090
@apollodavis4090 Жыл бұрын
this only works since f1 after the first iteration is one step behind s1 (because of the cycle) thats why it works only when f1 is 2 steps faster not 3 or 4
@MitrankShah
@MitrankShah 2 жыл бұрын
Hey Striver, I have a doubt here. We are starting with s = s + 1 and f = f + 2 so initially 'f' is ahead when compared to 's' so why can't we put a condition that when 'f' is behind 's', a cycle would be existing, and we would then return true. Possible?
@ganavin3423
@ganavin3423 2 жыл бұрын
I also have the same doubt
@Rohitkumar-me1uc
@Rohitkumar-me1uc 2 жыл бұрын
but how in linked list will you decide f is behind s? i think for this you have to do what striver is doing
@mriduljain1981
@mriduljain1981 2 жыл бұрын
bro you can do it, code will run completely fine bool hasCycle(ListNode *head) { if(head == NULL){ return false; } ListNode* slow = head; ListNode* fast = head; while(fast->next != NULL && fast->next->next != NULL){ slow = slow->next; fast = fast->next->next; if(slow >= fast){ return true; } } return false; } check it out
@fullysimplified7139
@fullysimplified7139 3 жыл бұрын
Hats off to you dude
@thallapakuteja2350
@thallapakuteja2350 2 жыл бұрын
will it catches if faster moves 3*slower ? is it possible for any faster=n*slower
@takeUforward
@takeUforward 2 жыл бұрын
Noh, it won’t collide then.
@kamranahmad8317
@kamranahmad8317 2 жыл бұрын
say the loop doesn't start from the end like it start from somewhere in the middle then this 2 pointer approach will fail? wouldn't it?
@satyamrai445
@satyamrai445 2 жыл бұрын
where can i get this sde problems sheet ??
@aryansinha1818
@aryansinha1818 2 жыл бұрын
2:52 to 4:13 advertisement
@giraffe4375
@giraffe4375 3 жыл бұрын
Thanks a lot striver
@harshgupta1913
@harshgupta1913 4 жыл бұрын
Understood. Please Rabin Karp and KMP lago
@poornimapatel3119
@poornimapatel3119 4 жыл бұрын
Bhaiya please answer 🙏, Tier 3 clg with EC can I get placement.
@poornimapatel3119
@poornimapatel3119 4 жыл бұрын
Bhaiya please
@savalalingeshreddy6750
@savalalingeshreddy6750 4 жыл бұрын
@@poornimapatel3119 I am also from EC tier 3 college
@yashodeepdhas8408
@yashodeepdhas8408 3 жыл бұрын
@@poornimapatel3119 bro u can ofc
@theparagbagga
@theparagbagga 2 жыл бұрын
THankyou so much
@democratcobra
@democratcobra 3 жыл бұрын
U r my Hero...
@rahul.parihar
@rahul.parihar 2 жыл бұрын
Node * fast = head; Node * slow = head; while(fast && fast->next){ fast = fast->next->next; slow = slow->next; if(slow == fast)return true; } return false; Easier && shorter code
@samayrathod7668
@samayrathod7668 2 жыл бұрын
why their is && in while loop it should be || ?
@UnKnown-id7ih
@UnKnown-id7ih 4 жыл бұрын
Bhiya it will good for me, if you cover this placement series topic wise. By the way love you bhiya.
@takeUforward
@takeUforward 4 жыл бұрын
Isn’t it getting covered topicwise
@shreyasingh1258
@shreyasingh1258 4 жыл бұрын
Bhaiya please make a video on starting of loop also !🙏
@takeUforward
@takeUforward 4 жыл бұрын
Will come soon
@shreyasingh1258
@shreyasingh1258 4 жыл бұрын
@@takeUforward you the bestt💯💯
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
thanks striver
@AyushKumar-ju9jj
@AyushKumar-ju9jj Жыл бұрын
Follow Up Question: WHY can we not use slow inside while loop instead of fast?? Anyone?
@SDE-wq4tl
@SDE-wq4tl 3 жыл бұрын
First Approach will fail if list contains duplicate node🤔
@bsaikumarreddy1413
@bsaikumarreddy1413 3 жыл бұрын
Nope bro.. We have to push entire node rather than pushing node value into hash table.
@krishnasai3367
@krishnasai3367 3 жыл бұрын
@@bsaikumarreddy1413 so what?
@shamshersinghrawat9377
@shamshersinghrawat9377 2 жыл бұрын
@@bsaikumarreddy1413 can you please share the code of 1st approach
@shawshak2148
@shawshak2148 Жыл бұрын
Understood
@shashankgsharma0901
@shashankgsharma0901 6 ай бұрын
Understood!
@rishabhagrahari3770
@rishabhagrahari3770 4 жыл бұрын
Tier 3 college ke non CS branch se tech companies me placements ( off-campus) ho skta h ? Plz bhaiya log help karo 🙏🙏
@aryankumar3018
@aryankumar3018 Жыл бұрын
understood
@noonetalkseither4315
@noonetalkseither4315 Жыл бұрын
am the only one who gets the snoring voice at the back ground ? 🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣
@devjain1691
@devjain1691 2 жыл бұрын
Understooooooooooooooooooooooooooooooooooooooooooooooood
@ashwinvarma9349
@ashwinvarma9349 4 жыл бұрын
If cp is generally done with c++ then why teach java?
@takeUforward
@takeUforward 4 жыл бұрын
Just in case..
@ambermittal8174
@ambermittal8174 4 жыл бұрын
Bhaiya maths weak h kya me competitive coding karlu ga
@Amritanjali
@Amritanjali 4 жыл бұрын
yes
@aashishprateek5030
@aashishprateek5030 3 жыл бұрын
could we also use another approach wherein we ask the interviewer the max number of nodes in the linked list, and then what we do is, we keep a count variable initialised to 0, and then run a while loop until the count variable doesn't exceed the maximum number of nodes in the linked list, and keep on incrementing the header to the next node, and if at any point the header reaches NULL, then there is no cycle, otherwise a cycle will exist if the while loop is completed and NULL is not found. it uses O(N) time complexity and O(1) space complexity as you are only using count variable. the code for it is as follows: bool hasCycle(ListNode *head) { int count=0; while(countnext; count++; } return true; }
@nikhil_squats
@nikhil_squats 3 жыл бұрын
But how to detect where cycle starts??
@takeUforward
@takeUforward 3 жыл бұрын
I have another video on that.. check it out..
@judgebot7353
@judgebot7353 Жыл бұрын
unique algo
@lokeshvikram6192
@lokeshvikram6192 4 жыл бұрын
Thx bro
@ujjwaljain9780
@ujjwaljain9780 3 жыл бұрын
sir im using this condition in while and all code is same but solution in not accepted ... ... while(fast.next!=null){ .... ...... } please till me why it is not working
@kunal_chand
@kunal_chand 4 жыл бұрын
Where is the 2nd & 3rd problem Explanation ?
@takeUforward
@takeUforward 4 жыл бұрын
I explained them but forgot to turn on the screen recording 🤣 realised now when I was searching for the recording to edit. So pushed this to second and moved the other two to bottom. Will come up next day 🙂
@spongycode
@spongycode 4 жыл бұрын
@@takeUforward 😁
@letsexplore7590
@letsexplore7590 4 жыл бұрын
😅😅
@md.imrankhan6912
@md.imrankhan6912 Жыл бұрын
magician
@aasthajain4210
@aasthajain4210 4 жыл бұрын
✨💫
@surajbugade
@surajbugade 2 жыл бұрын
Us
Check for Palindromic Linked List | Snapdeal | Adobe | Amazon
13:40
take U forward
Рет қаралды 135 М.
Binary Search Animated
7:00
Dreams of Code
Рет қаралды 41 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
Data Structures: Cycles in a Linked List
5:19
HackerRank
Рет қаралды 167 М.
Reverse Nodes in k-Group | Among the toughest problems of LinkedList
20:05
Flattening of a Linked List | Amazon | Microsoft
20:50
take U forward
Рет қаралды 152 М.
Middle of the Linked List | EP 5
9:24
Fraz
Рет қаралды 36 М.
N meetings In One Room | Greedy Algorithm
18:00
take U forward
Рет қаралды 199 М.
The Black Box Method: How to Learn Hard Concepts Quickly
14:09
Colin Galen
Рет қаралды 1,1 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН