L15. Find the length of the Loop in LinkedList

  Рет қаралды 61,099

take U forward

take U forward

Күн бұрын

Пікірлер: 80
@zainabkhan-ym3kh
@zainabkhan-ym3kh 9 ай бұрын
I am addicted to go through the procedure of brute to optimize solution and sometime its frustrating when someone just mug up the solution without giving insights like you striver.
@AshishSingh-he2qo
@AshishSingh-he2qo 3 ай бұрын
Solved with optimal approach without seeing the solution ❤❤🎉
@endlesslearning26
@endlesslearning26 3 ай бұрын
i solved via brute force but couldn't think for the optimal solution sadly
@KARTHIKEYANR-b1n
@KARTHIKEYANR-b1n Ай бұрын
I solved using brute
@MaheshPatil-of1zy
@MaheshPatil-of1zy 4 ай бұрын
Solved By Own by watching your previous Lecture.
@Benstokes4444
@Benstokes4444 4 ай бұрын
optimized wala ya brute force
@shamanthhegde
@shamanthhegde 3 ай бұрын
Let me know pls whether you did the same or something different //Time Complexity: O(n) //Space Complexity: O(1) public class Solution { public int lengthOfCycle(ListNode head) { if(head == null || head.next == null) return 0; int count = 0; ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null) { fast = fast.next.next; slow = slow.next; count++; if(fast == slow) { return count; } } return 0; } }
@ritikshandilya7075
@ritikshandilya7075 7 ай бұрын
Immense approach for any beginner , its crisp as well as detailed . Thanks Striver for being Striver
@stith_pragya
@stith_pragya 6 ай бұрын
Understood.......Thank You So Much for this wonderful video........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@_CodeLifeChronicles_
@_CodeLifeChronicles_ 5 ай бұрын
the best tutorials ever
@ArpitDharmshaktu
@ArpitDharmshaktu 4 ай бұрын
i think we can decrease the time cmplx. a little bit more by just taking the length from starting to the coliision node i.e (L1+d) where L1 is the distance from starting to the slow node when it reaches the junction and d is the distance of that slow node to the coliision of nodes for better understanding of how L1+d can be the length of the loop check the last video which is finding the starting point of loop.
@sandiprath4896
@sandiprath4896 3 ай бұрын
Yes, the same thought also comes to mind, but I don't know why it's not working.
@AkOp-bf9vm
@AkOp-bf9vm 3 ай бұрын
it will not work for LL 1->2->3->4->5->6->7->8->9->8 here length of Loop is 2 but slow will be more than 2. THIS CODE WILL ONLY WORK WHEN SLOW POINTER REACH STARTING NODE OF THE LOOP AND FAST POINTER SHOULD NOT PASS THE CURRENT NODE OF SLOW
@vamsikrishnagannamaneni912
@vamsikrishnagannamaneni912 16 күн бұрын
distance from slow node to collision of nodes is infact 'd' but how will find it? After finding the starting point of cycle will you traverse the slow again till collision? This will yield same time complexity
@hareshnayak7302
@hareshnayak7302 7 ай бұрын
Understood,thanks striver for this amazing video.
@VisibleNasir
@VisibleNasir 9 ай бұрын
Thank you 💟 🙃 I'm doing dsa with striver ! But sorry because I downloaded all Videos 😅
@satyamgupta-pv2ib
@satyamgupta-pv2ib 3 ай бұрын
cnt not go at 10:02 slow =fast node at the last , cnt start with one cnt stop at one step back where slow=fast node ......code is write But explaination is awersome Thankyou Striver
@SouravSahu-f1y
@SouravSahu-f1y 7 ай бұрын
Sir really grateful to you 🙏 just one question if the heap videos will come? As not liking any other tutor after learning from you
@BadalDesai-s7h
@BadalDesai-s7h 9 ай бұрын
Hey Striver just wondering, In the last video about finding the loop starting point you said that the loop length will be "L1 + d" so can't we just return the number of steps slow moved before we detect the loop (fast==slow) ??
@pravinthakur7791
@pravinthakur7791 7 ай бұрын
int lengthOfLoop(Node *head) { // Write your code here Node* slow = head; Node* fast = head; while(fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; if(slow == fast) { coutnext; } cout
@shamanthhegde
@shamanthhegde 3 ай бұрын
We can simplify it actually Just do the old approach of detect cycle... and whenever the fast and slow meets...the distance travelled by the slow pointer becomes the size of the loop //Time Complexity: O(n) //Space Complexity: O(1) public class Solution { public int lengthOfCycle(ListNode head) { if(head == null || head.next == null) return 0; int count = 0; ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null) { fast = fast.next.next; slow = slow.next; count++; if(fast == slow) { return count; } } return 0; } }
@YourCodeVerse
@YourCodeVerse 10 ай бұрын
Understood✅🔥🔥
@NazeerBashaShaik
@NazeerBashaShaik 6 ай бұрын
Understood, thank you.
@akshaysmart6257
@akshaysmart6257 3 ай бұрын
So far After watching these 15 lectures on LinkedList why I feel more confident in LinkedList than Arrays😅😅?? Is the linkedlist concept itself easy or I didn't concentrate more on Arrays. I even completed the Arrays playlist 2 times😒
@ManishLakkavatri
@ManishLakkavatri 11 ай бұрын
Understood! but what will be the Time Complexity??
@govindasharma9077
@govindasharma9077 11 ай бұрын
Time complexity will be O(N*length of loop) but we can consider it somewhere around O(N) and space will be O(1)
@IITians
@IITians 10 ай бұрын
@@govindasharma9077 TC: O(N + length of loop)
@abhaymandal4903
@abhaymandal4903 9 ай бұрын
O(2n)
@abhaymandal4903
@abhaymandal4903 9 ай бұрын
​@@govindasharma9077 it will not (n*length of loop ) as for every value of n there is not seperate loop. There is linear relation . So it will be O(2n)
@2amCoder
@2amCoder 9 ай бұрын
O(len)+O(len of loop)
@krishkothari8634
@krishkothari8634 6 ай бұрын
int lengthOfLoop(Node *head) { // Write your code here Node* slow = head; Node* fast = head; while(fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if(slow == fast) { int cnt = 0; do { fast = fast->next; cnt++; } while(fast != slow); return cnt; } } return 0; }
@NonameNoname-f2t
@NonameNoname-f2t 8 ай бұрын
superb
@selene8721
@selene8721 7 ай бұрын
Thank you so much!!
@RajNamdev_19
@RajNamdev_19 3 ай бұрын
Understood;
@ಪ್ರಯತ್ನ-ಞ3ಧ
@ಪ್ರಯತ್ನ-ಞ3ಧ 2 ай бұрын
7:05 superbike sound 😌.... Yes guys I got distracted..
@striverdaaadi
@striverdaaadi 10 ай бұрын
awesome bro
@TS-oj3vd
@TS-oj3vd 7 күн бұрын
time complexity?
@dewanandkumar8589
@dewanandkumar8589 6 ай бұрын
Understood
@abhinavprabhat4418
@abhinavprabhat4418 11 ай бұрын
int lengthOfLoop(Node *head) { Node* fast = head; Node* slow = head; int cnt = 0; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if (slow == fast) { do { cnt++; fast = fast->next; } while (slow != fast); return cnt; } } return 0; }
@nishant4595
@nishant4595 2 ай бұрын
was able to solve this.....
@iamnottech8918
@iamnottech8918 9 ай бұрын
complexity ?
@priyanshuchaudhary6469
@priyanshuchaudhary6469 6 ай бұрын
i have a question,in your last videos you have taught us that the total distance of loop will be d+L1 where L1 will be the same distance from the head to that node which is the starting node of the loop and d is the distance from the starting node of the loop where slow was present and when slow pointer meets with fast pointer in the clockwise direction. So why can't we only calculate the total distance from starting to the node where slow pointer meets the fast pointer int count=0; Node slow=head,fast=head; while(fast!=null && fast.next!==null) { slow=slow.next; fast=fast.next.next; count++; if(slow==fast) { return count; } } i have just written main code Someone plz explain
@priyadarsimishra7909
@priyadarsimishra7909 6 ай бұрын
You need to account for one edge case where the length of the loop is not necessarily always L1 + d. This is because L1 could be large and the loop could be small. The initial L1 distance is always divisible to the length of the loop (you can just draw some cases out and see this).
@shamanthhegde
@shamanthhegde 3 ай бұрын
@@priyadarsimishra7909can you give me a test case because as far i see even for the case you mentioned it seems to work fine
@priyadarsimishra7909
@priyadarsimishra7909 3 ай бұрын
@@shamanthhegde I am saying that it is not always L1 + d because the length of the loop could be only 2 or 3 nodes whereas L1 is like 5 nodes, so in that case you just loop till fast reaches itself again. That would be the loop length else if fast reaches slow then we have the loop length as well.
@AkOp-bf9vm
@AkOp-bf9vm 3 ай бұрын
@@shamanthhegde it will not work for LL 1->2->3->4->5->6->7->8->9->8 here length of Loop is 2 but slow will be more than 2. THIS CODE WILL ONLY WORK WHEN SLOW POINTER REACH STARTING NODE OF THE LOOP AND FAST POINTER SHOULD NOT PASS THE CURRENT NODE OF SLOW
@Trollvideos1154
@Trollvideos1154 11 ай бұрын
nice dude
@utkarshiitbhu4204
@utkarshiitbhu4204 5 ай бұрын
but when they collide suppose from starting of loop their length is d and the remaining is L1 which is length from starting link list to starting loop so loop is L1+D whish is the amount slow pointer covers???
@shamanthhegde2820
@shamanthhegde2820 3 ай бұрын
Crct bro I too applied the same logic and it works
@AkOp-bf9vm
@AkOp-bf9vm 3 ай бұрын
WRONG BRO
@shamanthhegde2820
@shamanthhegde2820 3 ай бұрын
@@AkOp-bf9vm why?
@AkOp-bf9vm
@AkOp-bf9vm 3 ай бұрын
@@shamanthhegde2820 check for the test case 1->2->3->4->5->6->7->8->9->8 HERE LENGTH OF LOOP IS 2 BUT LENGTH OF SLOW POINTER MOVES FROM START IS GREATER THAN 2
@PixelPioneer92800
@PixelPioneer92800 11 ай бұрын
understood
@AK-wc1cv
@AK-wc1cv 5 ай бұрын
Can someone please explain why he wrote fast=fast.next before and inside while loop
@madhu_mohanreddy
@madhu_mohanreddy 4 ай бұрын
before to move the fast to next node and next in the while loop to start the count from there!!
@PremShinde-p4q
@PremShinde-p4q 7 ай бұрын
What will be time complexity of optimal approach ? Will it be O(2N)? Plz answer
@shaiksoofi3741
@shaiksoofi3741 4 ай бұрын
understoodd
@SamyakSharma-oy1bv
@SamyakSharma-oy1bv 16 күн бұрын
respect++;
@rajatshukla2605
@rajatshukla2605 Ай бұрын
Undestood
@successvibes1604
@successvibes1604 11 ай бұрын
int lengthOfLoop(Node *head) { Node* slow = head; Node* fast = head; while(fast!=NULL and fast->next!=NULL){ fast = fast->next->next; slow = slow->next; if(fast == slow){ int ct = 1; slow = head; //slow = slow->next; while(slow != fast){ slow = slow->next; ct++; } return ct; } } return NULL; why this code is not working as distance from head to the point where fast and slow collide show be equal to the length of loop because in last lec where we calculated the starting point of loop we have used this concept slow = head than move till fast collide with slow someone plz explain
@successvibes1604
@successvibes1604 11 ай бұрын
someone plz answer 🤔🤔
@GauravSharma-ij1ym
@GauravSharma-ij1ym 9 ай бұрын
​@@successvibes1604 just take a Linked List with just 3 elements in loop and atleast 8 elements in linear chain before loop . Dry run it and u will get your amswer
@aarzookhunger9487
@aarzookhunger9487 8 ай бұрын
it shouldn't be calculated from slow = head *after you initialised the ct* instead do slow = slow->next as in the loop that we've detected , it will move slow one step ahead than fast and calculate the length of loop until we again hit fast. Else the code looks correct.
@ashishpradhan6250
@ashishpradhan6250 5 ай бұрын
arigato
@rinokamura1152
@rinokamura1152 10 ай бұрын
Can someone send brute force method 's code .
@vineetverma3964
@vineetverma3964 10 ай бұрын
int findLoopLength(ListNode* head) { std::unordered_map nodeMap; // Map to store node addresses and their corresponding indices ListNode* current = head; int index = 0; while (current != nullptr) { // Check if the current node is already in the map if (nodeMap.find(current) != nodeMap.end()) { // Loop detected, calculate the length int loopStartIndex = nodeMap[current]; return index - loopStartIndex; } // Add the current node to the map nodeMap[current] = index; // Move to the next node current = current->next; index++; } // No loop found return 0; }
@rinokamura1152
@rinokamura1152 10 ай бұрын
@@vineetverma3964 Love ya bruh
@iamnoob7593
@iamnoob7593 8 ай бұрын
US
@cenacr007
@cenacr007 6 ай бұрын
us
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
public int countNodeInloop{ Node head){ Node fast =head,slow=head; int cnt=1; while(fast.next!=null&&fast.next.next!=null){ slow=slow.next; fast=fast.next.next; if(slow==fast){ fast=fast.next; while(slow!=fast){ cnt++; fast =fast.next; } return cnt; } } return 0; }
@pradipkumarmukhi
@pradipkumarmukhi 6 ай бұрын
Understood
@abhinanda7049
@abhinanda7049 7 ай бұрын
understood
@abhishekprasad010
@abhishekprasad010 6 ай бұрын
Understood
@hardikpatel352
@hardikpatel352 6 ай бұрын
understood
@meghasharma4875
@meghasharma4875 2 ай бұрын
understood
@rutujashelke4208
@rutujashelke4208 3 ай бұрын
Understood
@abhinavabhi3568
@abhinavabhi3568 Ай бұрын
Understood
L16. Delete the middle node of the LinkedList
16:36
take U forward
Рет қаралды 47 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 98 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 45 МЛН
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
Find the length of loop in linked list | GeeksforGeeks
4:04
GeeksforGeeks
Рет қаралды 16 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 736 М.
L12. Find the intersection point of Y LinkedList
32:05
take U forward
Рет қаралды 80 М.
L11. Add 1 to a number represented by LinkedList
25:28
take U forward
Рет қаралды 76 М.
From 2.6 GPA to $200K Microsoft Software Engineer
17:21
Sundas Khalid
Рет қаралды 102 М.
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,3 МЛН
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 340 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3 МЛН