We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 260bdba commit 343ec44Copy full SHA for 343ec44
Data Strucrures/Middle traversing linked list.py
@@ -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