Skip to content

Commit 343ec44

Browse files
committed
modified
1 parent 260bdba commit 343ec44

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
class LinkedList:
6+
def __init__(self):
7+
self.start = None
8+
def push(self, value):
9+
new_node = Node(value)
10+
new_node.next = self.start
11+
self.start = new_node
12+
def printMiddle(self):
13+
slow_ptr = self.start
14+
fast_ptr = self.start
15+
if self.start is not None:
16+
while (fast_ptr is not None and fast_ptr.next is not None):
17+
fast_ptr = fast_ptr.next.next
18+
slow_ptr = slow_ptr.next
19+
print("The middle element is: ", slow_ptr.data)
20+
list1 = LinkedList()
21+
list1.push(5)
22+
list1.push(9)
23+
list1.push(12)
24+
list1.push(20)
25+
list1.push(3)
26+
list1.push(14)
27+
list1.printMiddle()

0 commit comments

Comments
 (0)