Remove Loop from Linked List | Floyd's Algorithm

  Рет қаралды 12,308

Knowledge Center

Knowledge Center

5 жыл бұрын

Remove Loop or Cycle from Linked List using Floyd's Cycle Detection Algorithm.
Find First Element of Loop using Floyd's Algo: • Find beginning of cycl...
Why Floyd's Loop Detection Algorithm Works: • Why Floyd's Cycle Dete...
Detect Cycle in Linked List: • Detect Loop in Linked ...
Code is available at:
neuralnetworksai.blogspot.com...
github link: github.com/KnowledgeCenterYou...
**** Best Books For Data Structures & Algorithms for Interviews:
1. Cracking the Coding Interview: amzn.to/2WeO3eO
2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
Programming Interview Questions
Crack coding interview
#Remove #Loop #Linked-List

Пікірлер: 17
@karansagar7870
@karansagar7870 4 жыл бұрын
well explained
@prateeksrivastava9
@prateeksrivastava9 4 жыл бұрын
Great Explanation
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
Thanks.
@amankumarchourasiya2279
@amankumarchourasiya2279 4 жыл бұрын
There is a flaw in your code. It will fail for the test case when the starting point of the loop is the head itself.
@treyquattro
@treyquattro 3 жыл бұрын
No it won't. This is the easiest test case - all pointers point at the same node so you can't help but get the right node to reset the next pointer!
@coderaspirant
@coderaspirant 3 жыл бұрын
Ya got your point.
@codeandtalk6
@codeandtalk6 3 жыл бұрын
Can I get code for the test case u are talking about
@rahulbajetha4057
@rahulbajetha4057 3 жыл бұрын
if(slw==head) // for circular linklist { while(slw->next!=head) slw=slw->next; slw->next=NULL; }
@sugandhm2666
@sugandhm2666 2 жыл бұрын
Yes his approach doesnt work when starting point of loop is header node. However to handle that condition we can modify his code by running a while for fast node until its not slow. Below is the code public static void removeLoop(Node head){ // code here // remove the loop without losing any nodes Node slow = head; Node fast = head; while(fast!=null && fast.next!=null) { slow = slow.next; fast = fast.next.next; if(slow==fast) { if(slow==head && fast==head) { while(fast.next!=head) fast = fast.next; } else { slow = head; while(slow.next!=fast.next) { slow = slow.next; fast = fast.next; } } fast.next = null; } } }
@RaGa_BABA
@RaGa_BABA 2 жыл бұрын
will it work if end of loop contains head.ie: 1->2->3->4->5 and 5 has address of 1.
@adityahridyam7698
@adityahridyam7698 3 жыл бұрын
arre why this is working explain that.
@trilokgyaan5371
@trilokgyaan5371 3 жыл бұрын
Unhone ek anya video me ye explain kiya hai. Why Floyd's cycle detection algorithm works. kzfaq.info/get/bejne/edljftpy2NfVnmg.html
@adityahridyam7698
@adityahridyam7698 3 жыл бұрын
@@trilokgyaan5371 dhanyawaad✌
@treyquattro
@treyquattro 3 жыл бұрын
why the negativity? Substance is solid and the code is correct. Note that we actually get real code in an IDE, not scribbles on the screen like with many computer science channels.
@amankumarchourasiya2279
@amankumarchourasiya2279 4 жыл бұрын
Modified version of the code is void removeLoop(Node* list) { Node *slow_p = list, *fast_p = list; while (slow_p && fast_p && fast_p->next) { slow_p = slow_p->next; fast_p = fast_p->next->next; if (slow_p == fast_p) { slow_p = list; if(fast_p == list){ while(slow_p->next != list){ slow_p = slow_p->next; } slow_p->next = NULL; } else{ while(slow_p->next != fast_p->next){ slow_p = slow_p->next; fast_p = fast_p->next; } fast_p->next = NULL; } } } }
@harinderkaur8587
@harinderkaur8587 5 жыл бұрын
well explained. could u pls provide the code in link
@Bohomann
@Bohomann 3 жыл бұрын
terrible delivery
Swap nodes in Linked List without Copy | Programming Interview | C++
20:48
Sliding Window Technique - Algorithmic Mental Models
36:45
Ryan Schachte
Рет қаралды 337 М.
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 19 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 39 МЛН
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 9 МЛН
Dijkstra's Algorithm - Computerphile
10:43
Computerphile
Рет қаралды 1,3 МЛН
Two Pointer Algorithm | Two Sum Problem | Solve DS Problems in O(N) Time
19:18
JAVAAID - Coding Interview Preparation
Рет қаралды 112 М.
Stop, Intel’s Already Dead!
13:47
Linus Tech Tips
Рет қаралды 435 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 141 М.
Top 6 Coding Interview Concepts (Data Structures & Algorithms)
10:51
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 265 М.
Sliding Window Technique + 4 Questions - Algorithms
27:25
QuanticDev
Рет қаралды 111 М.
But what are Hamming codes? The origin of error correction
20:05
3Blue1Brown
Рет қаралды 2,3 МЛН
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 19 МЛН