Write a program to Delete loop in a linked list
Pattern: Fast and slow pointers
Find the loop's first node as in Linked List Cycle II, walk once around to the node whose next is that first node, and set its next to null. The list becomes a straight list again.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Given the head of a linked list that may contain a loop, remove the loop by setting one next pointer to null, so that
the list keeps every node in the same order and ends properly. If there is no loop, leave the list as it is. For
example, 3 → 2 → 0 → -4 with -4 pointing back to 2 becomes the straight list 3 → 2 → 0 → -4.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
removeLoop is this guide's own. The tests print the list from the head afterwards, so a loop left in place would never
finish printing.
Think first
- Which single pointer must change?
- How do you find the first node of the loop?
- From there, how do you find the node that points back to it?
Hints
Brute force, and why it fails
A hash set of visited nodes finds the first repeated next and cuts it: O(n) memory. Floyd's method plus one lap needs
O(1).
The key insight
Removing a loop is "find where it starts, then find who points there". Floyd's method answers the first, and one lap answers the second.
Dry run
The list is 3 → 2 → 0 → -4, and -4 points back to 2. Floyd's method finds that the loop starts at 2.
The plan
find slow == fast with Floyd's method; if fast reaches the end: return
a = head
while a != slow: a = a.next; slow = slow.next # a is the loop's first node
last = a
while last.next != a: last = last.next
last.next = null
Complexity
- Time
O(n)- Space
O(1)
Floyd's phases and the lap are each O(n).
Edge cases
- No loop: unchanged.
- A node pointing to itself: its next becomes null.
- The loop starts at the head: the last node of the list points to null.
I've built my logic, show the code
Java
class Solution {
public void removeLoop(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) break;
}
if (fast == null || fast.next == null) return; // no loop
ListNode a = head; // find the loop start, as in Linked List Cycle II
while (a != slow) { a = a.next; slow = slow.next; }
ListNode last = a; // walk once round to the node just before the start
while (last.next != a) last = last.next;
last.next = null; // break the loop there
}
}C++
class Solution {
public:
void removeLoop(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (fast == NULL || fast->next == NULL) return; // no loop
ListNode* a = head; // find the loop start, as in Linked List Cycle II
while (a != slow) { a = a->next; slow = slow->next; }
ListNode* last = a; // walk once round to the node just before the start
while (last->next != a) last = last->next;
last->next = NULL; // break the loop there
}
};Python
from lib import ListNode
class Solution:
def removeLoop(self, head: ListNode) -> None:
slow = fast = head
while fast is not None and fast.next is not None:
slow, fast = slow.next, fast.next.next
if slow is fast:
break
if fast is None or fast.next is None:
return # no loop
a = head # find the loop start, as in Linked List Cycle II
while a is not slow:
a, slow = a.next, slow.next
last = a # walk once round to the node just before the start
while last.next is not a:
last = last.next
last.next = None # break the loop thereCommon mistakes
- Cutting at the meeting point, which may break the list in the wrong place and lose nodes.
- Walking the lap from the meeting point and stopping at the wrong node.
- Forgetting the no-loop case, where
fastreached the end.