Linked List Cycle II | Complete Intuition | Leetcode - 142 | Amazon | Microsoft

  Рет қаралды 7,984

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 56
@oqant0424
@oqant0424 Жыл бұрын
the intuition explanation part was the best.....learnt a lot...thanks❤❤
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Thanks bhai, Below are the two Java implementations: Brute Force - Using a HashSet: Time Complexity O(N) and space O(N): Passes all test cases public class Solution { public ListNode detectCycle(ListNode head) { Set set = new HashSet(); while(head != null && set.add(head)) { head = head.next; } return head; } } Using fast and slow pointer : Time Complexity O(N) and space O(1): Beats 100% public class Solution { public ListNode detectCycle(ListNode head) { if(head == null || head.next == null) return null; ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) { break; } } //either slow is equal to fast or fast is null if(slow != fast) { return null; } ListNode p = head; while(p != slow) { p = p.next; slow = slow.next; } return p; } }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Tysm ❤️
@gurudassulebhavikar
@gurudassulebhavikar 9 ай бұрын
i got this question in todays interviews. Luckily I gave the exactly same answer as I have solved this before. But failed to explain the intuition behind it. Now I understood why we start new pointer from head and keep incrementing slow pointer until these to meet. Thanks bhai.. you are doing a great job.
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Intuition behind slow and fast pointer is amazing understood thanks
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@kratiyadav378
@kratiyadav378 2 ай бұрын
wonderful explanation !
@drbullah1388
@drbullah1388 Жыл бұрын
After understanding the concept from the video, i wrote this code ListNode *detectCycle(ListNode *head) { ListNode* slow = head; ListNode* fast = head; ListNode* aux = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(slow == fast){ while(slow != aux){ aux = aux->next; slow = slow->next; } return aux; } } return NULL; }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
So glad 🙂
@aws_handles
@aws_handles 8 ай бұрын
This is an extremely popular qn. You did justice with the detailed explanation as always. Thanks a lot for your efforts
@thekindspill
@thekindspill Жыл бұрын
Intuition was really well explained. Well done king
@RaviPatel-bi2wq
@RaviPatel-bi2wq Жыл бұрын
Amazing Explanation bro👏
@AmitBarua-gp8cg
@AmitBarua-gp8cg 8 ай бұрын
I love this channel for practicing question and building logic helps me a lot ❤❤❤❤
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
So Happy to hear that!❤️🙏
@nikhil4062
@nikhil4062 Жыл бұрын
Tysm for this amazing explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Nikhil ❤️
@tutuimam3381
@tutuimam3381 Жыл бұрын
Amazing explanation 👍
@bhargavijethva7958
@bhargavijethva7958 8 ай бұрын
Nice Explanation!👏👏
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️❤️🙏🙏
@rahulsati5819
@rahulsati5819 9 ай бұрын
better than striver
@wearevacationuncoverers
@wearevacationuncoverers 8 ай бұрын
true and many other KZbinrs too
@Lakshya-f4l
@Lakshya-f4l 4 ай бұрын
Thanks a ton sir !
@VineetKumar-pj1bk
@VineetKumar-pj1bk Жыл бұрын
As always beauty 😍
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
java solution code : public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow=head; ListNode fast=head; ListNode temp=head; ListNode entry=head; if(temp==null || temp.next==null ) return null; while(fast.next!=null && fast.next.next!=null) { slow=slow.next; fast=fast.next.next; if(slow==fast) { while(slow.val!=entry.val) { slow=slow.next; entry=entry.next; } return slow; } //temp=temp.next; } return null; } }
@aripact
@aripact Ай бұрын
No words only love!
@codestorywithMIK
@codestorywithMIK Ай бұрын
This means a lot. Thank you 😇♥️
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
The intuition 🔥
@nitkarshchourasia2406
@nitkarshchourasia2406 12 күн бұрын
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *slow = head; ListNode *fast = head; if (head == NULL || head->next == NULL) { return NULL; } while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if (slow == fast) { ListNode *temp = head; while (slow != temp) { slow = slow->next; temp = temp->next; } return slow; } } return NULL; } }; HERE IS THE IMPROVED VERSION OF THE CODE. HOPE YOU LIKE IT.
@nagmakhan3165
@nagmakhan3165 Жыл бұрын
Thanks for the intuition
@gauravbanerjee2898
@gauravbanerjee2898 8 ай бұрын
Thank you bhaiya ❤️
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️❤️🙏🙏
@oqant0424
@oqant0424 Жыл бұрын
bhaiya at #5:05 aapne jo video banane ki bat kahi thi.....please make a video on that
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Noted. Will soon upload that.
@biswajitpanigrahi-pp2pl
@biswajitpanigrahi-pp2pl Жыл бұрын
@@codestorywithMIK Yes please explain why is it so that if fast and slow pointer coincides then a cycle exists?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure. Let me make a video soon on that as well
@DevOpskagyaan
@DevOpskagyaan 8 ай бұрын
I was asked this in an interview for internship
@M.m554
@M.m554 Жыл бұрын
sir plz make a playlist on concepts of set and hashmap in java.....
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi there, Can you suggest what things you want in that playlist ? Do you mean solving qns on set ? Or understanding set , hash etc ?
@M.m554
@M.m554 Жыл бұрын
@@codestorywithMIK understanding hash and set.
@priyajaiwal8072
@priyajaiwal8072 Ай бұрын
If someone can help understand me this, how come nk-l2 is =left over part of the loop, we subtracted one l2, but nk itself will contain more l2 right?
@oqant0424
@oqant0424 Жыл бұрын
9th March 2023__DAY 1 brute force code: class Solution { public: ListNode *detectCycle(ListNode *head) { set s; ListNode * temp=head; while(temp){ if(s.find(temp)!=s.end())return temp; else{ s.insert(temp); } temp=temp->next; } return NULL; } };
@the_only_one9415
@the_only_one9415 5 ай бұрын
By using Hashset solution what if there are duplicate elements but no cycle?
@AmanSingh-sx1tz
@AmanSingh-sx1tz 2 ай бұрын
I have a doubt, jab tak fast n*k rounds laga lega loop ke, tab tak slow pointer bhi toh n/2*k rounds laga lega loop mei, toh vo n/2*k wala term ham consider kyu nhi kr rhe hai??
@divyangdheer7292
@divyangdheer7292 Жыл бұрын
0:20 college mai ghanta kuch nhi pdhaya int collegelife = 4; while(collegelife--){ file(); assignment(); attendance(); }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Unfortunately college these days are a waste of time
@vaibhavgupta973
@vaibhavgupta973 8 ай бұрын
noice
@amitsaini1181
@amitsaini1181 7 ай бұрын
In Linked List when slow pointer move by one and fast pointer move by two then fast pointer be like: " Chal chal tu apni main tujhe pehchan lunga"😂😂😂😂
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
😂
@prudhvirajmacherla9854
@prudhvirajmacherla9854 Жыл бұрын
Bro do you use iPad for teaching
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes PrudhviRaj
@AnandKumar-kz3ls
@AnandKumar-kz3ls Жыл бұрын
slow pointer can complete n1 circles then it will meet with fast pointer how can we prove that slow pointer will meet fast pointer without completing the cycle
@Rajput_Jatin
@Rajput_Jatin 5 ай бұрын
because distance b/w slow & fast is decr. by 1 when inside cycle. lets say when slowp reached start of cycle fastp was k nodes away from it. so fast will reach it in k moves. & since k is always < cycle len , so fast will always reach slow within first lap.
@shresthgupta2670
@shresthgupta2670 6 ай бұрын
hamare college asisa koi scene hii nahi hai
@souravjoshi2293
@souravjoshi2293 8 ай бұрын
Using same code to check if cycle is present or not. class Solution { public: bool hasCycle(ListNode *head) { if(!head) return false; //no element if(!(head->next)) return false; //one element ListNode* slow = head; ListNode* fast = head; bool foundCycle = false; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if(slow == fast){ foundCycle = true; break; } } return foundCycle; } };
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
public class solution { public list Node detect Cycle (list Node head){ list Node slow =head,fast =head; while (fast!=null &&fast. next!=null){ slow =slow. next; fast =fast.next.next; if(slow ==fast)break; } if(fast==null ||fast. next ==null)return null; while (head!=slow){ head =head. next; slow =slow. next; } return head; } }; Tc0(n); sc0(1); Thanks again take care your health;🎉❤
@Smashkartzz
@Smashkartzz 11 ай бұрын
time limit exceeded
L14. Detect a loop or cycle in LinkedList | With proof and Intuition
20:26
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
Odd Even Linked List - (Microsoft, Amazon) : Explanation ➕ Live Coding
22:04
Lec -9  VSEPR model JEE advance/mains (ultra basic)
2:00:27
⚡️NEWS | RUBLE COLLAPSE | STRIKE ON CRIMEA | PUTIN IN KAZAKHSTAN
10:34
Ходорковский LIVE
Рет қаралды 196 М.
Pointers in C / C++ [Full Course]
3:47:23
freeCodeCamp.org
Рет қаралды 4,5 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН