C++ Solution: github.com/striver79/SDESheet/blob/main/detectStartOfLoop_C%2B%2B Java Solution: github.com/striver79/SDESheet/blob/main/detectStartofLoop_Java Do check me at Instagram(Striver_79) to know all about the success stories, and attend the live interactions that I keep doing :)
@PriyanshuKumar-bx9oy2 жыл бұрын
Sorry but I didn't understood how you have written L1=nc-L2 ?
@anuragshekhawat Жыл бұрын
Slow pointer can never make more than two turns in loop.
@anuragshekhawat Жыл бұрын
At any point if slow pointer enters loop, after travelling half distance of loop, slow pointer bound to meet fast. I am assuming slow pointer takes 1 step and fast takes two steps.
@allmighty20004 жыл бұрын
intuition that When I first heard that we can’t use a hash table , The first thing came at my mind , it’s like A is a person he is going into a forest , roaming around , and then he observed that he is lost by remembering that he just visited that place , but we are told not to use hash table , So it’s just like A is a person with memory loss , so let’s assume B is his brother he also come the same way to find his brother but he will be also lost too , And their speed must differ so that they find out each other (collide) But I thought my idea is wrong as it’s not possible to calculate their starting position by collision theory , Now I am seeing , that don’t be lazy imaging , just grab pen and paper and do some work to find out relations , @Raj bhaia thanks 🙏
@takeUforward4 жыл бұрын
Glad :)
@scorcism. Жыл бұрын
damm extraordinary explanation
@abhay9994 Жыл бұрын
Dear Striver, I wanted to send you a heartfelt thank you for your tireless dedication to teaching DSA for free. Your selflessness and passion for helping students with their job interview preparation have made a significant impact on my life and countless others. I am incredibly grateful for the knowledge and confidence you have imparted to us. Thank you for everything you do!
@vijaykumar-b6i7t6 ай бұрын
@takeUforward hii, i think there is a slight change in the intution equation. it should be like 2(l1+l2+k(c) )=l1+l2+n(c). because pointers are not bound to collide in the first iteration it self that you mentioned in the video but was missing in the equation, writing the equation in this way seems better to me. overall its good, and thanks for the playlist, it's wonderful.
instead of nC we may write 1 cycle because as fast pointer completes two cycles slow pointer will complete atleast one and that we are adding in L2.....
@AZ-kh9ex4 жыл бұрын
Irony is none of those 7* or red coders would have ever joined such a subscription plan ( as unacademy's ) to reach where they are ...
@amanshrivastava9083 жыл бұрын
Yeah Striver himself claimed in his latest course related Cp course post he learned everything googling and finding online tutorials. These course is for those people who can't go without spoonfeeding and cannot self learn from themselves
@aryanverma78003 жыл бұрын
@@amanshrivastava908 there is something called time buddy
@crushed_oreos2 жыл бұрын
Thanks for explaining the intuitions so clearly Striver, really makes coding up the solution a bit easier. ❤
@kaustav074 жыл бұрын
Finally the intuition is clear.
@ritikatiwari37532 жыл бұрын
you are such an amazing person you have saved so many lives god bless you!!! lots and lots of love and power to you.
@wanderer_ankur4 жыл бұрын
This question's approach was very similar to the first question of SDE sheet. Thank you strive for helping so much :)
@prakashjha72384 ай бұрын
Thanks for the math behind the algorithm! I also wish to be guiding person to many.
@rahulgovindkumar31053 жыл бұрын
That was an awesome intuition explanation .......... really really really thank you so much for your efforts
@byomakeshpanda8055 Жыл бұрын
That intuition part 🤩. Thanks Striver ❤❤
@gatishmehta20392 жыл бұрын
This is exactly what I was looking for. Thank you so much! :)
@ranasauravsingh2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@jayachandra6774 жыл бұрын
I solved this today in the morning, I luckily assumed that the head pointer will always meet the slow pointer at some point. When I tried it for a couple of examples, I understood that my assumption was true and my code got accepted. Thank you for your thorough explanation bhaiya!
@takeUforward4 жыл бұрын
Was the intuition clear?
@aysams2 Жыл бұрын
@@takeUforward yes sir, it certainly was.
@amitpaunikar34084 жыл бұрын
Hi ... Good explanation of all topics Some linked list question is remaining Clone a list with next and random pointer Detect a cycle and remove loop from list Waiting for this topic video
@takeUforward4 жыл бұрын
Everything will come :), with the help of this problem you can remove the loop also, just keep track of the previous pointer to slow when slow meets the entry pointer, so you can easily point the previous pointer to NULL, and you were successful in removing the loop :)
@satyamgupta6030 Жыл бұрын
thanks alot striver ur explaination's are very good and easy to understand.
@NeerajSharma-mz4es3 жыл бұрын
Want to understand the concept in minimal time with all approaches this is the channel
@priyanshumahato52703 жыл бұрын
dude this explanation was the best soo far : )
@rishabhrathore1727 Жыл бұрын
you make look these algorithms questions soo easy man i am in love with your content
@subhamrajgarhia25245 ай бұрын
THIS IS MORE OPTIMAL APPROACH public class Solution { public ListNode detectCycle(ListNode head) { Map map = new HashMap(); ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) { ListNode p1 = head; ListNode p2 = slow; while(p1 != p2) { p1 = p1.next; p2 = p2.next; } return p1; } } return null; } }
@sudiptadas2806 Жыл бұрын
You have explained the problem in a very easy way. thanks a lot
@gowdhamt16512 жыл бұрын
Perfect explanation about the Intuition , Thank you!!
@tirthpatel20 Жыл бұрын
nice intuition #striver especially for the entry pointer and slow pointer point to find cycle start point.
@AKASH-sw9bs2 жыл бұрын
what a clean explanation .. i also watched the video from some of the other youtube tutorials but they represented the same thing in a much more complex way... Thanks brother for making the algorithm so simpler to understand .
@stevewilsonraj Жыл бұрын
thank you striver !!!
@priyaj99082 жыл бұрын
Your explanation is very clear and understandable. Thank you so much😊
@shikharathaur66643 жыл бұрын
Such a wonderful explanation of intuition. Thanks for the efforts !
@AkhileshKumar-lv7yv5 ай бұрын
Thank you for the explanation!
@thelimit97194 жыл бұрын
in interviews, apart from algos , do they ask qns like implement ds like a deque, circular linked list etc ?
@willturner34403 жыл бұрын
Yup
@kaichang81863 ай бұрын
understood, thanks for the great explanation
@abhinilnath190011 ай бұрын
BRILLIANT INTUTION
@ragnarT34 жыл бұрын
There is some kind of flute like sound coming... 2:45-2:50 here it is clearly noticeable. Great work bhai... Just thought of letting u knw about the noise 😀
@takeUforward4 жыл бұрын
oh chair awaaz krta front back jb krta hun
@lightninghunterCR Жыл бұрын
Understood!! And damn that intuition reminds me of Discrete Mathematics!
@tanushree01069 ай бұрын
Thank you so much raj bhaiya..may god always bless you😇
@rohithreddy89762 жыл бұрын
This is the only video where I understood intuition. Thank you so much, Bro
@VaishnaviNigam4 жыл бұрын
BESTTTTT EXPLAINATION🤩🤩🤩🤩🤩🤩
@binodbinod75374 жыл бұрын
Thank you soo much for the video ❤️🙏
@nagame859 Жыл бұрын
One of the best explanations of yours! 🎩📴
@sahilgoyals4 жыл бұрын
Super Clear, Thank you so much sir 🤩
@SATYAPRAKASH-ph1kk10 ай бұрын
very well explained in detail and also the code is very to understand and code
@sayantaniguha85192 жыл бұрын
10:03 Even if we write *entry=head* separately, it wont affect space complexity. Cuz, we are using 'entry' pointer to traverse a part of the list . We are not inserting nodes into it . Right ?
@arhitakundu5846 Жыл бұрын
The best explanation on youtube!
@meshkatuddinahammed Жыл бұрын
Bro, when about the string playlist is coming? Eagerly waiting for it. Thanks!
@kamalnayan53314 жыл бұрын
Thanks for the video .🌞
@Steve_Chandan4 жыл бұрын
really i like your explanation all the time.😊😊😊
@kewaltakhellambam7710 Жыл бұрын
such an intricate solution! Nice
@krishanusaha5212 Жыл бұрын
Great Explanation 🔥
@hardikagarwal39383 жыл бұрын
L2 is the dist covered by slow pointer, which may include some cycles and the dist from entry point to point of intersection. So how the L1 = nC-L2 be certainly equal to the remaining dist of the cycle?
@anuragjana1112 жыл бұрын
Same question
@DTMAPunitSingh Жыл бұрын
You can add n1C to slow pointer and you will get same result
@adityavishalsingh8624 Жыл бұрын
12:55 Doesn't matter if slow have covered Cycles or not .. let's assume it have some cycles (x turns) and for sure if slow have cycle so fast will cover cycle(y turns) more than the slow one... So to make things convenient you can make x to zero and corresponding update y to (y-x) .. hope it now make sense.
@devangsingh29503 жыл бұрын
I have a doubt if we take a fast pointer travelling twice the distance than slow pointer, they are bound to meet and then we can find the starting node of the linked list by travelling from the meeting point. What if they move at random speeds fast = 3*slow, then also they are bound to meet(Using hash table probing logic) but then how can we find the start of the cycle then?
@rohangangwar66043 жыл бұрын
bhaiya jitna mja algo me ni aaya usse kahi jada intution se aaya...thank u so much bhaiya
@uptonogood3004 жыл бұрын
was able to solve it with little different approach even before seeing the solution . thanks to the previous videos in the playlist !
@sourabhchoudhary72893 жыл бұрын
will you please share you approach/code ??
@Hound773 жыл бұрын
why are we checking for fast->next && fast->next->next is there any edge case which we will miss if we use fast->next && fast?
@sumekagarwal82293 жыл бұрын
Which hash table to use in the naive approach? set or a map? and how to store it...........set? class Solution { public: ListNode *detectCycle(ListNode *head) { set s; ListNode * tmp = head; while(tmp!=NULL){ if(s.find(tmp)!=s.end()){ return tmp; } s.insert(tmp); tmp=tmp->next; } return NULL; } }; please correct this.🤶
@dhanashreegodase44453 жыл бұрын
Thanks for the effort
@prathamjain893711 ай бұрын
Since time complexity is O(n) means we are sure that slow pointer and fast will meet before the slow traverses all the elements .I mean how are we sure that slow will not loop more than once in cycle before meeting fast . Can someone please explain
@tanishkumar5827 Жыл бұрын
well explained !
@shivamgupta3363 жыл бұрын
I think you have not considered one condition if fast and slow pointer meets at starting point. In this case entry and slow are giving wrong answer .
@sourabhkumar61943 жыл бұрын
That verification part explained very well
@subhadeepsadhukhan2875 Жыл бұрын
Thank you
@vickyarora73803 жыл бұрын
Very helpful!
@dreamyme5432 жыл бұрын
Great explanation , Thank you:)
@welcometoc.s.easpirants8 ай бұрын
You Rock Man.
@jackisname6825 Жыл бұрын
i have a question striver if suppose slow and fast pointer will collide at node(8) then i guess slow and entry pointer will never point to the same node according to the operations applied on slow and entry pointer?? please answer this question
@PriyanshuKumar-bx9oy2 жыл бұрын
Can anybody elaborate how L1 becomes equal to nc-L2 in general not from equation?
@mukuljain4087 Жыл бұрын
but why are you not considering the cycle of slow pointer
@ritikashishodia6752 жыл бұрын
Bhaiya hashing mind m ai O(1) nahi Hua usme ek problem ai us cycle s jo extra node h uski vjh s . Agr slow or fast us cycle k start pr khade ho to same honge kuch us loop m travel krne k bd
@ritikashishodia6752 жыл бұрын
Thank u bhaiya bs ek point nahi such payi last vala ki Jo gap arra h start or phuchne m cycle k vo entry s pura ho skta h 🤩🤩🤩 thanks a lot bhaiya....
@AbhishekKumar-td5zu4 жыл бұрын
Tqsm bhaiya ♥️♥️
@Yag116 Жыл бұрын
cant we add a condition to optimize TC as at 6:48 slow already collides with fast pointer ?
@auroshisray91403 жыл бұрын
Great explanation as always.
@-ShangsitNath2 жыл бұрын
why distance travelled by fast pointer is twice the distance travelled by slow pointer?? Is it bcoz, fast pointer is taking 2 steps at a time?
@crushed_oreos2 жыл бұрын
yes, so if we do n iterations, fast pointer covers 2*n steps and slow pointer covers 1*n steps
@shashankgsharma09016 ай бұрын
Understood!
@gauravtripathi94563 жыл бұрын
Wow, such a good explanation of the intuition behind the algorithm. Thanks for the effort !
@letsexplore75903 жыл бұрын
Thanks Bhaiya thanks 🙏
@bhavyagarg97632 жыл бұрын
The explanations are very precisely explained and hence easy to understand . Thanks a lot for the amazing content. Keep up the good work.
@sadabhalim58064 жыл бұрын
Love you brother :)
@bhaveshsoni66843 жыл бұрын
I used one approach to solve the same problem with exactly o(n) complexity. In this approach, I changed the value of every node with my name. if I find my name again, then it is the Cycle starting point. Is it the right approach?
@takeUforward3 жыл бұрын
Yes it is, but modifying given data is not allowed generally if it can be done without it.
@gayathrijaini23863 жыл бұрын
Is it nC-l2 or C-l2. Bcoz one cycle length is c right? 🤔
@sathwikabhignan18627 ай бұрын
exactly, I also have the same doubt here
@HDbIce-oj3wv4 жыл бұрын
Thanks bahiya 👍
@be_like__prateek4 жыл бұрын
@take U forward Bhai how many iterations will fast pointer loop within the loop in the worst case ? Is the answer 2 ??
@accepted58563 жыл бұрын
same doubt
@yatinarora12523 жыл бұрын
Yes it is taking twice the number of iterations so it will take 2 iterations kind of and regarding it may happen in wrost case they won't find any cyclr
@shafeymalik5334 Жыл бұрын
can anyone answer is it possible if the entry and slow keeps revolving anf they never meet?
@anilyadav-ky1bg3 жыл бұрын
Hi Bhayia, in outer loop while i was like
@gnaneshwarkandukuri67333 жыл бұрын
how fast pointer dist=2* slow pointer....?
@astrid_6622 Жыл бұрын
how do we know that nc-L2 is the distance needed to be travelled by slow pointer ? i am assuming the fast pointer only takes on turn more to reach upto slow pointer
@sathwikabhignan18627 ай бұрын
yeah how de we know that
@madanmohan56612 жыл бұрын
Can someone tell me after detecting the loop how we can remove it from the list?
@shivamchauhan029nitjsr4 Жыл бұрын
bhaiya agar input mein non-cyclic linkedList ajati h (in case) to while loop SEGGS dega i guess.
@jaihari14042 жыл бұрын
hey hey hey good explanation,but i hava an problem where can i learn this type of "maths":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>>>>>>>>>>>>>>>>>>>
@practice88443 жыл бұрын
Best teacher ever
@gigachadkartik3 жыл бұрын
mazedaar, nice video
@rudrasimhaa2694 жыл бұрын
Anybody know who is the Genius that first come up with this idea?
@gourabsinha4 жыл бұрын
Why not we are making the fast pointer jumps three nodes?
@takeUforward4 жыл бұрын
it been stated in the previous video... already said that in the video
@gourabsinha4 жыл бұрын
@@takeUforward but it's for only fast pointer jumping two times that doesn't clear my doubt. Let's say a vehicle A is having v speed and other vehicle B which is having 3v speed and distance between them is D where A is in front of B. Then also B will catch A after some time for sure as speed of B > A. Then why aren't we considering that one. It will be faster know?
@takeUforward4 жыл бұрын
No the chances of colliding sooner is with fast jumping twice :)
@thelimit97194 жыл бұрын
@@gourabsinha correct me if im wrong . i think its not guarenteed with 3 jumps they collide. for eg: t=0, posA=3, posB=0, speedA=1, speedB=3 t=1, posA=4, posB=3 t=2, posA=5,posB=6 now impossible to collide. but u take any example of ur choice and use fast as 2jumps ahead, they r bound to collide
@TomJerry-bp9ig4 жыл бұрын
Awsm❤
@amritraj6584 жыл бұрын
Bhaiya mai core branch se hu 1st year...kya mai IT Related company me ja sakta hu as a SDE, mai start kar diya programming sikhna ...... plz reply 🙏
@ShubhP-t2c Жыл бұрын
5:23
@rahulnavgire24613 жыл бұрын
you are great
@judgebot7353 Жыл бұрын
unique algo
@shyamjeegupta61732 жыл бұрын
would not the TC of first approach be O(NlogN), logN for searching the given address is present in hash table or not ?? Thanks
@crushed_oreos2 жыл бұрын
in c++ time complexity for searching an element in unordered set is O(1)