Python solution for middle of linked list.
Intuition\nMy first thought? Let me iterate throught the linked list. If the number of elements are odd, I return (middle_element/2)+1 th element and if there a
# Intuition\nMy first thought? Let me iterate throught the linked list. If the number of elements are odd, I return (middle_element/2)+1 th element and if there are even numbers I return the (middle_element)/2 element. \nIt\'s space complexity would be O(N)+O(N/2)\nIt\'s time complexity would be O(1)\nHowever there is a more optimised solution also called as the ***tortoise method or two pointers***.\n\n# Approach\nWe use two pointers here. One is the slow tortoise and another is the fast tortoise (Both are pointers). For every one step that the slow tortoise moves, the fast tortoise moves two steps! This means that when fast tortoise reaches the end of he linked list the slow tortoise is at the middle of the linked list. Voila! There\'s no need to traverse the list again. We can simply return the slow tortoise (aka slow pointer).\n\n(Credit ~Striver\'s)\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```python3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        slow = head \n        fast = head\n        while(fast != None and fast.next != None):\n            slow = slow.next\n            fast = fast.next.next\n        return slow\n```
['Python3']