L15. Find the length of the Loop in LinkedList

Поділитися
Вставка
  • Опубліковано 27 сер 2024
  • Problem Link: tinyurl.com/5n...
    Entire LL Sheet: takeuforward.o...
    Check our A2Z DSA Course: takeuforward.o...
    Please do give us a like, and subscribe to us if you are new to our channel.
    Do follow us on our socials: linktr.ee/take...

КОМЕНТАРІ • 71

  • @zainabkhan-ym3kh
    @zainabkhan-ym3kh 6 місяців тому +17

    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.

  • @ritikshandilya7075
    @ritikshandilya7075 4 місяці тому

    Immense approach for any beginner , its crisp as well as detailed . Thanks Striver for being Striver

  • @AshishSingh-he2qo
    @AshishSingh-he2qo 14 днів тому +1

    Solved with optimal approach without seeing the solution ❤❤🎉

    • @endlesslearning26
      @endlesslearning26 11 днів тому

      i solved via brute force but couldn't think for the optimal solution sadly

  • @ArpitDharmshaktu
    @ArpitDharmshaktu Місяць тому +1

    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 19 днів тому

      Yes, the same thought also comes to mind, but I don't know why it's not working.

    • @AkOp-bf9vm
      @AkOp-bf9vm 9 днів тому

      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

  • @satyamgupta-pv2ib
    @satyamgupta-pv2ib 10 днів тому +1

    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

  • @stith_pragya
    @stith_pragya 3 місяці тому +2

    Understood.......Thank You So Much for this wonderful video........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @MaheshPatil-of1zy
    @MaheshPatil-of1zy Місяць тому +1

    Solved By Own by watching your previous Lecture.

    • @Benstokes4444
      @Benstokes4444 Місяць тому

      optimized wala ya brute force

    • @shamanthhegde
      @shamanthhegde 26 днів тому

      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;
      }
      }

  • @hareshnayak7302
    @hareshnayak7302 4 місяці тому +1

    Understood,thanks striver for this amazing video.

  • @_CodeLifeChronicles_
    @_CodeLifeChronicles_ 2 місяці тому

    the best tutorials
    ever

  • @user-ff4ny5pm9m
    @user-ff4ny5pm9m 6 місяців тому +3

    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 4 місяці тому +2

      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 26 днів тому

      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;
      }
      }

  • @VisibleNasir
    @VisibleNasir 6 місяців тому

    Thank you 💟 🙃 I'm doing dsa with striver !
    But sorry because I downloaded all Videos 😅

  • @user-bf9hn4sw1n
    @user-bf9hn4sw1n 4 місяці тому

    Sir really grateful to you 🙏 just one question if the heap videos will come? As not liking any other tutor after learning from you

  • @RajNamdev_19
    @RajNamdev_19 22 дні тому +1

    Understood;

  • @akshaysmart6257
    @akshaysmart6257 23 дні тому

    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😒

  • @rutujashelke4208
    @rutujashelke4208 3 дні тому

    Understood

  • @krishkothari8634
    @krishkothari8634 3 місяці тому +1

    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;
    }

  • @ManishLakkavatri
    @ManishLakkavatri 8 місяців тому +2

    Understood! but what will be the Time Complexity??

    • @govindasharma9077
      @govindasharma9077 8 місяців тому

      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 8 місяців тому +6

      @@govindasharma9077 TC: O(N + length of loop)

    • @abhaymandal4903
      @abhaymandal4903 7 місяців тому +1

      O(2n)

    • @abhaymandal4903
      @abhaymandal4903 7 місяців тому +2

      ​@@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 6 місяців тому

      O(len)+O(len of loop)

  • @NazeerBashaShaik
    @NazeerBashaShaik 3 місяці тому

    Understood, thank you.

  • @YourCodeVerse
    @YourCodeVerse 7 місяців тому +1

    Understood✅🔥🔥

  • @user-ik3qu5uy5e
    @user-ik3qu5uy5e 5 місяців тому

    superb

  • @selene8721
    @selene8721 4 місяці тому

    Thank you so much!!

  • @priyanshuchaudhary6469
    @priyanshuchaudhary6469 3 місяці тому

    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 3 місяці тому +4

      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 26 днів тому

      @@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 26 днів тому +1

      @@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 9 днів тому

      @@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

  • @user-ke7fs7ds6h
    @user-ke7fs7ds6h 7 місяців тому

    awesome bro

  • @abhinavprabhat4418
    @abhinavprabhat4418 8 місяців тому +1

    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;
    }

  • @AK-wc1cv
    @AK-wc1cv 2 місяці тому

    Can someone please explain why he wrote fast=fast.next before and inside while loop

    • @madhu_mohanreddy
      @madhu_mohanreddy Місяць тому

      before to move the fast to next node and next in the while loop to start the count from there!!

  • @bharathns1361
    @bharathns1361 8 місяців тому

    nice dude

  • @hardikpatel352
    @hardikpatel352 3 місяці тому

    understood

  • @iamnottech8918
    @iamnottech8918 6 місяців тому

    complexity ?

  • @utkarshiitbhu4204
    @utkarshiitbhu4204 2 місяці тому

    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 26 днів тому

      Crct bro I too applied the same logic and it works

    • @AkOp-bf9vm
      @AkOp-bf9vm 9 днів тому

      WRONG BRO

    • @shamanthhegde2820
      @shamanthhegde2820 9 днів тому

      @@AkOp-bf9vm why?

    • @AkOp-bf9vm
      @AkOp-bf9vm 7 днів тому

      @@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

  • @shaiksoofi3741
    @shaiksoofi3741 2 місяці тому

    understoodd

  • @ashishpradhan6250
    @ashishpradhan6250 2 місяці тому +1

    arigato

  • @successvibes1604
    @successvibes1604 8 місяців тому +1

    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 8 місяців тому

      someone plz answer
      🤔🤔

    • @GauravSharma-ij1ym
      @GauravSharma-ij1ym 6 місяців тому

      ​@@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 5 місяців тому

      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.

  • @iamnoob7593
    @iamnoob7593 5 місяців тому

    US

  • @user-xp3hf2mb2u
    @user-xp3hf2mb2u 4 місяці тому

    What will be time complexity of optimal approach ?
    Will it be O(2N)?
    Plz answer

  • @rinokamura1152
    @rinokamura1152 7 місяців тому

    Can someone send brute force method 's code .

    • @vineetverma3964
      @vineetverma3964 7 місяців тому

      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 7 місяців тому

      @@vineetverma3964 Love ya bruh

  • @cenacr007
    @cenacr007 3 місяці тому

    us

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 9 місяців тому

    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 3 місяці тому

    Understood

  • @abhinanda7049
    @abhinanda7049 4 місяці тому

    understood

  • @dewanandkumar8589
    @dewanandkumar8589 3 місяці тому

    Understood

  • @abhishekprasad010
    @abhishekprasad010 3 місяці тому

    Understood

  • @PixelPioneer92800
    @PixelPioneer92800 8 місяців тому

    understood