the intuition explanation part was the best.....learnt a lot...thanks❤❤
@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 Жыл бұрын
Tysm ❤️
@gurudassulebhavikar9 ай бұрын
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 Жыл бұрын
Intuition behind slow and fast pointer is amazing understood thanks
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@kratiyadav3782 ай бұрын
wonderful explanation !
@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 Жыл бұрын
So glad 🙂
@aws_handles8 ай бұрын
This is an extremely popular qn. You did justice with the detailed explanation as always. Thanks a lot for your efforts
@thekindspill Жыл бұрын
Intuition was really well explained. Well done king
@RaviPatel-bi2wq Жыл бұрын
Amazing Explanation bro👏
@AmitBarua-gp8cg8 ай бұрын
I love this channel for practicing question and building logic helps me a lot ❤❤❤❤
/** * 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 Жыл бұрын
Thanks for the intuition
@gauravbanerjee28988 ай бұрын
Thank you bhaiya ❤️
@codestorywithMIK8 ай бұрын
❤️❤️🙏🙏
@oqant0424 Жыл бұрын
bhaiya at #5:05 aapne jo video banane ki bat kahi thi.....please make a video on that
@codestorywithMIK Жыл бұрын
Noted. Will soon upload that.
@biswajitpanigrahi-pp2pl Жыл бұрын
@@codestorywithMIK Yes please explain why is it so that if fast and slow pointer coincides then a cycle exists?
@codestorywithMIK Жыл бұрын
Sure. Let me make a video soon on that as well
@DevOpskagyaan8 ай бұрын
I was asked this in an interview for internship
@M.m554 Жыл бұрын
sir plz make a playlist on concepts of set and hashmap in java.....
@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 Жыл бұрын
@@codestorywithMIK understanding hash and set.
@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 Жыл бұрын
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_one94155 ай бұрын
By using Hashset solution what if there are duplicate elements but no cycle?
@AmanSingh-sx1tz2 ай бұрын
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 Жыл бұрын
0:20 college mai ghanta kuch nhi pdhaya int collegelife = 4; while(collegelife--){ file(); assignment(); attendance(); }
@codestorywithMIK Жыл бұрын
Unfortunately college these days are a waste of time
@vaibhavgupta9738 ай бұрын
noice
@amitsaini11817 ай бұрын
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"😂😂😂😂
@codestorywithMIK7 ай бұрын
😂
@prudhvirajmacherla9854 Жыл бұрын
Bro do you use iPad for teaching
@codestorywithMIK Жыл бұрын
Yes PrudhviRaj
@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_Jatin5 ай бұрын
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.
@shresthgupta26706 ай бұрын
hamare college asisa koi scene hii nahi hai
@souravjoshi22938 ай бұрын
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 Жыл бұрын
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;🎉❤