Middle of the linked list
December 5, 2022
linked-listProblem URL: Middle of the linked list
We will take two pointers, one will move one step at a time and the other will move two steps at a time. When the second pointer reaches the end of the list, the first pointer will be at the middle of the list.
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
Time complexity: O(n)
Space complexity: O(1)