How to remove loop from a Singly Linked List? | Floyd Cycle Detection Algorithm | (Implementation)

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • ►Full DSA Course - • Data Structures and Al...
    ►Follow me on Instagram - bit.ly/intrvwk...
    ►Follow me on LinkedIn - bit.ly/fllwlkdn
    ►Enroll in the complete course: bit.ly/3W4qthg
    ►Source Code - github.com/din...
    ►Download DSA Animation Slides - techready.in/c...
    ►Click here to subscribe - www.youtube.co...
    Watch all my playlist here:
    ►Data Structures and Algorithms Course playlist: • Data Structures and Al...
    ►Mastering JUnit 5 - www.youtube.co...
    ►Mastering Mockito 3 - • Mockito 3 Tutorials
    ►Analysis of Algorithms - • Analysis of Algorithms
    ►Linked List Data Structures - • Linked List Data Struc...
    ►Array Data Structures - • Playlist
    ►Stack Data Structure - • Stack Data Structure
    ►Queue Data Structure - • Queue Data Structure
    ►Binary Tree Data Structure - • Binary Tree Data Struc...
    ►Graph Data Structure - • Graph Data Structure
    ►Binary Heap Data Structure - • Binary Heap Data Struc...
    ►Trie Data Structure - • Trie Data Structure
    ►Dynamic Programming Algorithms - • Dynamic Programming Al...
    ►Hashing Data Structures - • Hashing Data Structures
    ►Sorting and Searching - • Sorting and Searching
    ►String Algorithms - • String Algorithms
    Want to land a software engineering job in the IT industry? This course - 'Visualizing Data Structures and Algorithms' is here to help. The course walks you through multiple Java algorithms, data structures problems, and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.
    The course covers in and outs of Data Structures and Algorithms in Java. Java is used as the programming language in the course. Students familiar with Javascript, Python, C#, C++, C, etc will also get to learn concepts without any difficulty. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers many interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -
    1. Animated slide. (To make visualization of algorithms faster)
    2. Coding algorithm on IDE.
    The course covers topics such as -
    0. Algorithm Analysis
    1. Arrays
    2. Matrix
    3. Singly Linked List
    4. Doubly Linked List
    5. Circular Singly Linked List
    6. Stacks
    7. Queues
    8. Binary Tree
    9. Binary Search Tree
    10. Graphs
    11. Priority Queues and Heaps
    12. Recursion
    13. Searching
    14. Sorting
    15. Strings
    16. Trie Data Structure
    17. Dynamic Programming
    and many more ...
    #dsa #algorithms #coding

Komentáře • 44

  • @itsdineshvaryani
    @itsdineshvaryani  Před 3 lety +1

    Please *Subscribe* and *Click Bell* 🔔🔔🔔 Icon for More Updates. To get *Data Structures and Algorithms* complete course for free please follow this link - czcams.com/play/PL6Zs6LgrJj3tDXv8a_elC6eT_4R5gfX4d.html

  • @Unknown-Stranger
    @Unknown-Stranger Před 3 lety +3

    Guys if you are getting wrong output when loop starts from 1st node of the Linked List, following code will cover this edge case(second removeLoop(slowPtr) method name I changed to makeLastNodeNull(slowPtr). :
    public void makeLastNodeNull(ListNode slowPtr) {
    ListNode temp = head;
    if(temp == slowPtr) {
    while(slowPtr.next != temp) {
    slowPtr = slowPtr.next;
    }
    slowPtr.next = null;
    return;
    }
    while(temp.next != slowPtr.next) {
    temp = temp.next;
    slowPtr = slowPtr.next;
    }
    slowPtr.next = null;
    }

  • @huzaifaimran9468
    @huzaifaimran9468 Před 3 lety +2

    Great video!!!
    But this method does not work if there is only 1 cycle in whole list at the start
    for example:
    to get around this we can use the start of loop method and check if slowPointer == startingPointer
    public void removeLoop()
    {
    //here nodes are = pointers
    //for single loop in whole list
    Node slowNode = detectLoop(); //returns slowPointer
    Node startingNode = detectStartOfLoop(); //returns the startingPointer
    if (slowNode == startingNode) // checking if slowNode = startingNode
    {
    while (slowNode.next!= startingNode) //looping until we have the last element of loop (before loop restarts)
    {
    slowNode = slowNode.next;
    }
    slowNode.next = null;
    return;
    }
    //for loop in a list with other values as well
    if (slowNode != null)
    {
    Node temp = head;
    while (temp.next != slowNode.next) { //getting the last element of loop before loop is restarted
    System.out.println(slowNode.data);
    temp = temp.next;
    slowNode = slowNode.next;
    }
    slowNode.next = null; //breaking the cycle before end and start of loop
    }
    }

  • @shubhamagarwal1434
    @shubhamagarwal1434 Před 2 lety +1

    Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over CZcams with java implementation and found this. Hats Off To You Man..Excellent Work. GOD BLESS YOU :)

  • @travel2explore627
    @travel2explore627 Před 2 lety +1

    Really thankful for these videos 👍

  • @sreddy8141
    @sreddy8141 Před 3 lety +1

    Thanks a lot for this wonderful series sir, Its really helpful. Kindly start series on programs based on these algo. Arrays, trees

  • @SatyamGupta-yq1pt
    @SatyamGupta-yq1pt Před 3 lety +2

    amazing content

  • @ascar66
    @ascar66 Před 2 lety +1

    Sir why we have two methods with same name? I mean we have public void removeLoop(){} and private void removeLoop(ListNode slowPtr){}. Is it overloading of the method? The same method name with different signatures?

    • @itsdineshvaryani
      @itsdineshvaryani  Před 2 lety

      You can give a different name as well !!!

    • @dailybloggerr
      @dailybloggerr Před 2 lety

      but in one method we are passing a argument so it treated as different

  • @jayasekharredditelluri1600

    sir your not explaining any time and space complixities here why?

  • @divyanshumaheshwari1880
    @divyanshumaheshwari1880 Před 2 lety +1

    sir why u created private method for removeloop(ListNode slowptr) method

  • @jagadeshreddyvankala6020
    @jagadeshreddyvankala6020 Před 2 lety +1

    i am not getting output after the code is growing longer

  • @vasudevparmar9876
    @vasudevparmar9876 Před 3 lety +2

    why did we name both function removeLoop

    • @itsdineshvaryani
      @itsdineshvaryani  Před 3 lety

      doesnt matter

    • @vasudevparmar9876
      @vasudevparmar9876 Před 3 lety +1

      @@itsdineshvaryani so isn't there any chances of recursive call?

    • @itsdineshvaryani
      @itsdineshvaryani  Před 3 lety +1

      Parameters are different in both methods ....

    • @vasudevparmar9876
      @vasudevparmar9876 Před 3 lety

      @@itsdineshvaryani this code fails when cycle starts from 1st node

    • @HariKrishnan-ks3dh
      @HariKrishnan-ks3dh Před 3 lety

      @@vasudevparmar9876
      if(temp == slowptr)
      {
      while(temp.next.next != slowptr.next)
      {
      temp = temp.next.next;
      slowptr = slowptr.next;
      if(temp.next.next == slowptr.next)
      {
      slowptr.next = null;
      }
      }
      }
      i solved the problem that if cycle occured in the head then it will be removed by this code

  • @abhishekranjan1094
    @abhishekranjan1094 Před 3 lety +1

    Nice sir

  • @MANISHYADAV-dd4ug
    @MANISHYADAV-dd4ug Před 3 lety +1

    ❤️❤️

  • @sai_kosuru14
    @sai_kosuru14 Před 3 lety

    Good work!

  • @jaspalsingh-mv5xh
    @jaspalsingh-mv5xh Před 3 lety +1

    It give me the output : 1->2->3->4->5->null

  • @jagadeshreddyvankala6020
    @jagadeshreddyvankala6020 Před 2 lety +1

    i am getting any output from here