πDay 9. Remove loop in Linked List π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You are given the head of a linked list that may contain a loop. A loop exists when the last node in the linked list connects back to a node in the same list. Your task is to remove the loop from the linked list (if it exists), ensuring the linked list remains intact.
Custom Input Format:
The input consists of:
A
head
node pointing to the start of a singly linked list.A
pos
value (1-based index) denoting the position of the node to which the last node points:If
pos = 0
, it means the last node points tonull
, indicating there is no loop.
Output:
The function returns true
if:
No loop exists in the list, or
The loop is successfully removed, and the linked list is restored.
Otherwise, it returns false
.
π Example Walkthrough:
Input:
head = 1 -> 3 -> 4, pos = 2
Output:
true
Explanation:
The linked list looks like this before removing the loop:
After removing the loop, the list becomes:
Input:
head = 1 -> 8 -> 3 -> 4, pos = 0
Output:
true
Explanation:
The linked list has no loop and remains unchanged.
Input:
head = 1 -> 2 -> 3 -> 4, pos = 1
Output:
true
Explanation:
The linked list looks like this before removing the loop:
After removing the loop, the list becomes:
Constraints:
1 β€ size of linked list β€ $10^5$
π― My Approach:
Detect Loop in the Linked List:
Use Floydβs Cycle-Finding Algorithm (Tortoise and Hare) to detect a loop in the linked list.
If the
slow
andfast
pointers meet, a loop is confirmed.
Find the Starting Node of the Loop:
Move one pointer back to the
head
and traverse both pointers (slow and fast) one step at a time.The node where they meet is the starting point of the loop.
Break the Loop:
Traverse the loop to find the node whose
next
pointer points to the starting node of the loop.Set this nodeβs
next
pointer tonull
, breaking the loop.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), where
N
is the size of the linked list. We traverse the linked list once to detect the loop and another time to remove it.Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of space (two pointers).
π Solution Code
Code (C++)
class Solution {
public:
void removeLoop(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next && (slow = slow->next) != (fast = fast->next->next));
if (!fast || !fast->next) return;
for (slow = head; slow != fast; slow = slow->next, fast = fast->next);
while (fast->next != slow) fast = fast->next;
fast->next = nullptr;
}
};
Code (Java)
class Solution {
public static void removeLoop(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null && (slow = slow.next) != (fast = fast.next.next));
if (fast == null || fast.next == null) return;
for (slow = head; slow != fast; slow = slow.next, fast = fast.next);
while (fast.next != slow) fast = fast.next;
fast.next = null;
}
}
Code (Python)
class Solution:
def removeLoop(self, head):
slow, fast = head, head
while fast and fast.next and (slow := slow.next) != (fast := fast.next.next): pass
if not fast or not fast.next: return
slow = head
while slow != fast: slow, fast = slow.next, fast.next
while fast.next != slow: fast = fast.next
fast.next = None
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated