Given: A singly linked list.
Goal: Return the middle node of the linked list.If there are two middle nodes, return the second one.
Example:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def middleNode(head: ListNode) -> ListNode:
slow = head
fast = head
while fast and fast.next:
slow = slow.next # move 1 step
fast = fast.next.next # move 2 steps
return slow