forked from Nandini13-rgb/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
75 lines (69 loc) · 1.55 KB
/
linked_list.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
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
if __name__ == '__main__':
llist = LinkedList()
print(llist)
print(llist.head)
node1 = Node(1)
llist.head = node1
print(llist)
#print(node1.data)
node2 = Node(2)
node1.next = node2
print(llist)
#print(node2.data)
node2.next = Node(3)
print(llist)
#print(node2.next.data)
temp = node1
while temp:
print(temp.data)
temp = temp.next
# print in reverse order
stack = []
temp = node1
while temp:
stack.append(temp.data)
temp = temp.next
while stack:
data = stack.pop()
print(data)
# print nth node
def nth_node():
index = 4
count = 0
temp = node1
while temp:
temp = temp.next
count += 1
if count == index:
print(Node(count).data)
#nth_node()
# print mid
def find_mid_iteration():
count = 0
temp = node1
while temp:
temp = temp.next
count += 1
if count%2 == 0:
print(count//2)
else:
print((count//2)+1)
find_mid_iteration()
def find_mid():
slow = node1
faster = node1
while faster.next:
faster = faster.next.next
if faster == None:
break
slow = slow.next
print(slow.data)
find_mid()
#node100 = Node(100)