-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathloop_detection_in_LL.py
94 lines (82 loc) · 2.65 KB
/
loop_detection_in_LL.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
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
87
88
89
90
91
92
93
94
'''
PROBLEM STATEMENT:
Given a linked list, the task is to find whether the linked list consists of
a loop or not.
The user will enter the elements in a linked list and will specify whether
the tail of the linked list is pointed to some other node or to NULL.
If the tail is pointing to NULL, the user will input -1 in 'pos' variable,
otherwise, the user will input the index of the node to which the tail pointer
will point.
'pos' variable is only used to create a linked list as per the data entered
by the user, and is not passed to the function for detecting a loop,
it is being detected by using Floyd's cycle detection algorithm.
'''
class Node:
# Constructor to initialize node
def __init__(self, val):
self.val = val
self.next = None
class LinkedList:
# Constructor to initialize head and variable pointer
def __init__(self):
self.head = None
self.var = None
# Function to create a linked list
def push(self, newval):
newnode = Node(newval)
if self.var:
self.var.next = newnode
self.var = newnode
else:
self.head = newnode
self.var = newnode
return newnode
# Function to create a loop
def CreateLoop(self, idx, last):
temp = self.head
if idx >= 0:
while idx > 0:
temp = temp.next
idx -= 1
last.next = temp
# Function for detecting loop using Floyd's cycle detection algorithm
def LoopDetect(self):
slow = self.head
fast = self.head
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
print("Loop Detected!")
return
print("Loop not found!")
# Driver Code
n = int(input("Enter the size of linked list: "))
ll = LinkedList()
print("Enter elements:")
for i in range(n):
# tail stores the last node of the linked list
tail = ll.push(int(input()))
# if the user wants to enter a linked list with a loop, pos specifies the index
# of the node which will be pointed by the tail
# For no loop, pos will take -1
pos = int(input("Index of node which is connected to tail, else enter -1: "))
ll.CreateLoop(pos, tail)
ll.LoopDetect()
'''
TEST CASE:
Input:
Enter the size of linked list: 5
Enter elements:
8
6
1
5
7
Index of node which is connected to tail, else enter -1: 2
Output:
Loop Detected!
TIME COMPLEXITY: O(n), to traverse the linked list
where 'n' denotes the length of the linked list.
SPACE COMPLEXITY: O(1), no extra space used.
'''