-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathRemove loop in Linked List.cpp
63 lines (54 loc) · 1.37 KB
/
Remove loop in Linked List.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Remove loop in Linked List
==========================
You are given a linked list of N nodes. Remove the loop from the linked list, if present.
Note: X is the position of the node to which the last node is connected to. If it is 0, then there is no loop.
Example 1:
Input:
N = 3
value[] = {1,3,4}
X = 2
Output: 1
Explanation: The link list looks like
1 -> 3 -> 4
^ |
|____|
A loop is present. If you remove it
successfully, the answer will be 1.
Example 2:
Input:
N = 4
value[] = {1,8,3,4}
X = 0
Output: 1
Explanation: The Linked list does not
contains any loop.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeLoop() which takes the head of the linked list as input parameter. Simply remove the loop in the list (if present) without disconnecting any nodes from the list. The driver code will print 1 if your code is correct.
Expected time complexity : O(n)
Expected auxiliary space : O(1)
Constraints:
1 ≤ N ≤ 104
*/
void removeLoop(Node *head)
{
auto slow = head, fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (fast == slow)
break;
}
if (!fast || !fast->next)
return;
slow = head;
while (slow != fast)
{
slow = slow->next;
fast = fast->next;
}
while (fast->next != slow)
fast = fast->next;
fast->next = NULL;
}