-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJan26 2025 POTD.java
34 lines (30 loc) · 1.33 KB
/
Jan26 2025 POTD.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Remove loop in Linked List
Difficulty: MediumAccuracy: 27.66%Submissions: 488K+Points: 4
Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to remove the loop from the linked list (if it exists).
Custom Input format:
A head of a singly linked list and a pos (1-based index) which denotes the position of the node to which the last node points to. If pos = 0, it means the last node points to null, indicating there is no loop.
The generated output will be true if there is no loop in list and other nodes in the list remain unchanged, otherwise, false.
class Solution {
// Function to remove a loop in the linked list.
public static void removeLoop(Node head) {
// code here
Node slow = head;
Node fast = head;
while(fast!= null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
Node last = slow;
while(last.next != fast){
last = last.next;
}
last.next = null;
}
}
}
}