-
Notifications
You must be signed in to change notification settings - Fork 1
/
Remove_loop_in_Linked_List.cpp
86 lines (69 loc) · 1.98 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Problem Statement:
-----------------
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)
*/
// Link --> https://practice.geeksforgeeks.org/problems/remove-loop-in-linked-list/1#
// Code:
class Solution
{
public:
void removeLoop(Node* head)
{
if(head == NULL)
return;
Node *slow = head;
Node *fast = head->next;
while(fast != slow)
{
slow = slow->next;
fast = fast->next->next;
if(!fast || !fast->next)
return;
}
// to count size of the loop.
int size = 1;
// set fast next to slow to count size.
fast = fast->next;
while(fast != slow)
{
size++;
fast = fast->next;
}
slow = head;
fast = head;
for(int i=0 ; i<size-1 ; i++)
fast = fast->next;
while(fast->next != slow)
{
fast = fast->next;
slow = slow->next;
}
fast->next = NULL;
}
};