-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlistloop.py
117 lines (99 loc) · 2.71 KB
/
linkedlistloop.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class node:
def __init__(self,data):
self.data=data
self.next=None
class linkedlist:
def __init__(self):
self.head=None
def printll(self):
if self.head==None:
print("empty linked list")
else:
n=self.head
while n is not None:
print(n.data)
n=n.next
def addbeg(self,data):
newnode=node(data)
newnode.next=self.head
self.head=newnode
def addend(self,data):
newnode=node(data)
if self.head==None:
self.head=newnode
else:
n=self.head
while n.next!=None: #this is imp n.next not ( n )
n=n.next
n.next=newnode
def addmiddle(self,data,x):
n=self.head
while n.data!=x :
n=n.next
newnode=node(data)
newnode.next=n.next
n.next=newnode
def looplinkedlist(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 found ")
return
return False
def reversell(self):
curr=self.head
prev=None
while curr!=None:
nex=curr.next
curr.next=prev
prev=curr
curr=nex
self.head=prev
def lllenght(self): #go till last node and add values
n=self.head
c=0
while n!=None:
c+=1
n=n.next
return c
def lldeleteend(self):
n=self.head
while n.next!=None:
if n.next.next==None:
n.next=None
else:
n=n.next
def isPalindrome(self):
fast = slow = self.head
# find the mid node
while fast and fast.next:
fast = fast.next.next
slow = slow.next
# reverse the second half
node = None
while slow:
nxt = slow.next
slow.next = node
node = slow
slow = nxt
# compare the first and second half nodes
n=self.head
while node: # while node and head:
if node.data != n.data:
return False
node = node.next
n = n.next
return True
n=linkedlist()
n.addbeg(20)
n.addbeg(30)
#n.head.next.next = n.head
n.addend(2)
n.reversell()
n.printll()
if n.looplinkedlist()==False:
print("Not Found")
#print(n.lllenght())