-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.py
30 lines (23 loc) · 858 Bytes
/
3.py
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
# Remove loop in Linked List
class Solution:
def delete_loop(self, head):
# trying to detect if loop is present or not
low = head
high = head
while high != None and high.next != None:
low = low.next
high = high.next.next
if low == high:
break
# if both low and high reach the head pointer, then we just need to move the high pointer
if low == head:
while high.next != low:
high = high.next
high.next = None
elif low == high: # checking of loop is present or not
low = head
# finding the starting point of the loop
while low.next != high.next:
low = low.next
high = high.next
high.next = None