VISUALGO//PROBLEMS
delete the middle node of a linked list · linked listmedium
LeetCode #2095
list
1
→
3
→
4
→
7
→
1
→
2
→
6
ELEMENTS
head
Python
def deleteMiddle(head: ListNode | None) -> ListNode | None:
    if head.next is None:
        return None  # one node: deleting it leaves nothing

    slow = head
    fast = head.next.next  # two ahead, so slow stops just before the middle
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    slow.next = slow.next.next  # unlink the middle node
    return head