forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiddle_Element.py
55 lines (43 loc) · 1.26 KB
/
Middle_Element.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
# Python3 program to find middle of linked list
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class contains a Node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Print the linked list
def printList(self):
node = self.head
while node:
print(str(node.data) + "->", end="")
node = node.next
print("NULL")
# Function that returns middle.
def printMiddle(self):
# Initialize two pointers, one will go one step a time (slow), another two at a time (fast)
slow = self.head
fast = self.head
# Iterate till fast's next is null (fast reaches end)
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# return the slow's data, which would be the middle element.
print("The middle element is ", slow.data)
# Code execution starts here
if __name__=='__main__':
# Start with the empty list
llist = LinkedList()
for i in range(5, 0, -1):
llist.push(i)
llist.printList()
llist.printMiddle()