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
@learnwithme77504 жыл бұрын
if there is a cycle in it , then it always include last node?
@yatinarora12523 жыл бұрын
@@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.
@ankushrai31553 жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
got the mistake 🤣🤣 as soon as i commented realised that I should use do while
@shiva_krishna_das3 жыл бұрын
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)
@mukundnayak73103 жыл бұрын
Thank you!
@yashlakade1793 жыл бұрын
Thanks a lot for these.......!!!
@altafmazhar77623 жыл бұрын
Thanks buddy its helpful
@swapnilparashare26702 жыл бұрын
Thank You for clear and simple explanation.
@pranav2882 жыл бұрын
what do yall get by copying from leetcode
@priyankagorkhe2870 Жыл бұрын
Whenever I need a clarity of a code this is the only platform which gives me transaprency in one shot! Keep it up!!
@jaswanthkosanam94814 жыл бұрын
Bruh never stop this series ❤️❤️❤️❤️❤️
@siddharth.chandani Жыл бұрын
Loved when striverr comes to the part of explaing the intuition behind the logic 🥰
@ajeetworking4 жыл бұрын
This series is helping a lot...thanks man
@aishwarya18954 жыл бұрын
Thanks a bunch vaeya... Literally u r a ray of hope for children who are not from iits
@takeUforward4 жыл бұрын
Thanks for commenting in every video and watching.
Thanks bro. Please don't stop this series, please. Please.
@shashankojha34524 жыл бұрын
Good Explaination✌❤ This problem can be modified a little if we want to find the starting point of the loop.
@takeUforward4 жыл бұрын
Its in the sde sheet so expect it to be out soon
@rahulgovindkumar31053 жыл бұрын
Thanks bro. Please don't stop this series, please. Please.Pleaseeeeeee
@ranasauravsingh2 жыл бұрын
UNDERSTOOD... !!! Thanks, striver for the video... :)
@kaichang81863 ай бұрын
understood, thanks for the great explanation
@anuragtiwari20984 жыл бұрын
The two pointer algorithm is called FLYOD'S LOOP DETECTION . Wud be great if u wud upload the detect and REMOVE loop part too
@takeUforward4 жыл бұрын
Coming soon
@bloke32534 жыл бұрын
It would be very helpful if you could upload head of the loop and remove cycle in a linkedlists Btw awesome explanation as always.
@takeUforward4 жыл бұрын
Coming soon
@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)
@deepesh16b2 жыл бұрын
Very good approach 🔥🔥🙏
@savalalingeshreddy67504 жыл бұрын
Thanks for the video bro good explanation Keep doing!
@samyak14092 жыл бұрын
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.
@GurankitSinghBehal2 жыл бұрын
You just cleared a massive doubt bro
@samyak14092 жыл бұрын
@@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. 🤦
@GurankitSinghBehal2 жыл бұрын
@@samyak1409 You actually wanted to write...... "fast.next==null"..... I understood it....... And now my question is solved
@samyak14092 жыл бұрын
@@GurankitSinghBehal oh okay!
@GurankitSinghBehal2 жыл бұрын
@@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
@prateekshourya4 жыл бұрын
Please don't join unacademy 🙏
@abhimanyu65343 жыл бұрын
Great explanation always 💯💯💥🙏
@SANCHITJAIN_3 жыл бұрын
great explanation bhaiya..loved it :)
@dreamyme5432 жыл бұрын
Great explanation as always. Thank you:)
@nileshsinha78694 жыл бұрын
Awesome👍.. please make day 6 question no. 3 video next 🙏
@takeUforward4 жыл бұрын
Coming soon
@rohitudasi15372 жыл бұрын
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 .
@rohitudasi15372 жыл бұрын
//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 Жыл бұрын
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 Жыл бұрын
why are we using two conditions in the while loop ?? if fast->next==NULL then fast->next->next will also be NULL only
@ganavin34232 жыл бұрын
why can't we use only fast.next.next !=null instead of two condition in while loop
@atharvakulkarni20243 жыл бұрын
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 Жыл бұрын
not O(1) space complexity I think bro if u track numbers from new space where u stored the numbers
@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; }
@loserfruit96633 жыл бұрын
Loved the intuition
@RahulKumar-ed1bt2 жыл бұрын
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.sinhmar4 жыл бұрын
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??
@takeUforward4 жыл бұрын
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 Жыл бұрын
Understood 💯💯💯
@aseemsharma46432 жыл бұрын
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?
@ashwanisingh49314 жыл бұрын
Awesome explanation 👌
@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
@MitrankShah2 жыл бұрын
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?
@ganavin34232 жыл бұрын
I also have the same doubt
@Rohitkumar-me1uc2 жыл бұрын
but how in linked list will you decide f is behind s? i think for this you have to do what striver is doing
@mriduljain19812 жыл бұрын
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
@fullysimplified71393 жыл бұрын
Hats off to you dude
@thallapakuteja23502 жыл бұрын
will it catches if faster moves 3*slower ? is it possible for any faster=n*slower
@takeUforward2 жыл бұрын
Noh, it won’t collide then.
@kamranahmad83172 жыл бұрын
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?
@satyamrai4452 жыл бұрын
where can i get this sde problems sheet ??
@aryansinha18182 жыл бұрын
2:52 to 4:13 advertisement
@giraffe43753 жыл бұрын
Thanks a lot striver
@harshgupta19134 жыл бұрын
Understood. Please Rabin Karp and KMP lago
@poornimapatel31194 жыл бұрын
Bhaiya please answer 🙏, Tier 3 clg with EC can I get placement.
@poornimapatel31194 жыл бұрын
Bhaiya please
@savalalingeshreddy67504 жыл бұрын
@@poornimapatel3119 I am also from EC tier 3 college
If cp is generally done with c++ then why teach java?
@takeUforward4 жыл бұрын
Just in case..
@ambermittal81744 жыл бұрын
Bhaiya maths weak h kya me competitive coding karlu ga
@Amritanjali4 жыл бұрын
yes
@aashishprateek50303 жыл бұрын
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_squats3 жыл бұрын
But how to detect where cycle starts??
@takeUforward3 жыл бұрын
I have another video on that.. check it out..
@judgebot7353 Жыл бұрын
unique algo
@lokeshvikram61924 жыл бұрын
Thx bro
@ujjwaljain97803 жыл бұрын
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_chand4 жыл бұрын
Where is the 2nd & 3rd problem Explanation ?
@takeUforward4 жыл бұрын
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 🙂