Skip to main content

Posts

Showing posts with the label The Crazy Programmer Detect and Remove Loop in a Linked List Detect and Remove Loop in a Linked List The Crazy Programmer

Detect and Remove Loop in a Linked List Vijay Sinha The Crazy Programmer

In this article, we will look at an interesting problem related to Linked List . ‘Given a Single Linked List, Remove Loop if there exists any’ . We will look at different ways to solve this problem and analyze the complexities of our approach. Now, To Remove a Loop in a Linked List, we need to first Detect a Loop in the linked list. If a Loop exists we remove it with our logic and print the elements of the list. So having a Loop in a Linked List means there is no such node having a NULL pointer or reference. In simpler terms, the Linked list has no end. Let us understand this with an example. Here, we have a Linked List with 5 nodes, Node 12 is the Head node. We can see a Loop exists in the list connecting nodes 25 and 99 . So we need to Remove this Loop such that the last node (25) points to NULL. After removal the List should look like this: We can see the last node now points to NULL and the loop no longer exists. Now, let us look at different approaches to solve the proble