How to remove loop from a Singly Linked List? | Floyd Cycle Detection Algorithm | (Implementation)
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
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
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;
}
This worked! Thanks a lot!
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
}
}
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 :)
Thanks !!!
Hii sir
Really thankful for these videos 👍
Welcome !!!
Thanks a lot for this wonderful series sir, Its really helpful. Kindly start series on programs based on these algo. Arrays, trees
Its started !!!
@@itsdineshvaryani ua the best sir🙂🙏
@@sreddy8141 thanks !!! Keep supporting.
amazing content
Thanks !!!
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?
You can give a different name as well !!!
but in one method we are passing a argument so it treated as different
sir your not explaining any time and space complixities here why?
Will try to add !!!
sir why u created private method for removeloop(ListNode slowptr) method
Extract logic out in diff method ...
@@itsdineshvaryani so we can use public method also to make it??
i am not getting output after the code is growing longer
check source code in description
why did we name both function removeLoop
doesnt matter
@@itsdineshvaryani so isn't there any chances of recursive call?
Parameters are different in both methods ....
@@itsdineshvaryani this code fails when cycle starts from 1st node
@@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
Nice sir
Thanks !!!
❤️❤️
Good work!
Thanks !!!
It give me the output : 1->2->3->4->5->null
It doesn't give me the 6th node
i am getting any output from here
check source code in description
check source code in description