-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversedlinkedlist.py
More file actions
63 lines (54 loc) · 1.33 KB
/
reversedlinkedlist.py
File metadata and controls
63 lines (54 loc) · 1.33 KB
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
class Node:
def __init__(self,data):
self.head=None
self.data=data
class linked_list:
def __init__(self):
self.head=None
def push(self,new_data):
new_node=Node(new_data)
new_node.next=self.head
self.head=new_node
def reverse(self):
prev=None
current=self.head
while current is not None:
next=current.next
current.next=prev
prev=current
current=next
self.head=prev
def printlist(self):
temp=self.head
while temp!=None:
print(temp.data)
temp=temp.next
def detectLoop(self):
slow=self.head
fast=self.head
while slow and fast and fast.next:
slow=slow.next
fast=fast.next.next
if slow == fast:
return True
return False
def delete(self,deleted):
temp=self.head
if temp.data == deleted:
self.head=temp.next
while temp != None :
if temp.data==deleted:
break
prev=temp
temp=temp.next
prev.next=temp.next
temp=None
llist = linked_list()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
llist.printlist()
llist.reverse()
llist.printlist()
print(llist.detectLoop())