diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 77577fc0..4e132715 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -11,6 +11,8 @@ - [Doubly](linked_lists/doubly.py) - [Singly](linked_lists/singly.py) - [Reverse List](linked_lists/reverse-linkedlist.py) +- [Middle Node](linked_lists/middle-node-linkedlist.py) + ## Dictionaries - [Two Sum](dictionaries/two-sum.py) diff --git a/algorithms/Python/linked_lists/middle-node-linkedlist.py b/algorithms/Python/linked_lists/middle-node-linkedlist.py new file mode 100644 index 00000000..e84f50f9 --- /dev/null +++ b/algorithms/Python/linked_lists/middle-node-linkedlist.py @@ -0,0 +1,43 @@ +#printing middle node element of linked list +#we are using two pointer method to get the solution + +class Node: + #Constructor to build node + def __init__(self, data): + self.data = data + self.next = None + +class LinkedList: + #Constructor to create head node + def __init__(self): + self.head = None + + #Function to push element at head (at beginning) + def push(self, data): + new_node = Node(data) + new_node.next = self.head + self.head = new_node + + #Function returns data of middle node + def middle_element(self): + if self.head is None: + return None + slow = self.head + fast = self.head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + return slow.data + +if __name__=='__main__': + ob = LinkedList() + ob.push(5) + ob.push(4) + ob.push(3) + ob.push(2) + ob.push(1) + print(ob.middle_element()) + + #list: 1->2->3->4->5 Hence middle element is 3 + +