Skip to main content

Middle of the Linked List

Find the middle node of a linked list Example: [1,2,3,4,5] => [3,4,5]

Given the head of a singly linked list, return its middle node, including the suffix of the list that begins at that node. If the list has two middle nodes, return the second one; for example, [1,2,3,4,5,6] returns the node whose suffix is [4,5,6]. The list contains 1 to 100 nodes, each with an integer value from 1 to 100.

Examples

You write

middle_node(head: Optional[ListNode])

  1. Example 1

    Input
    [1,2,3,4,5]
    returns
    [3,4,5]
  2. Example 2

    Input
    [1,2,3,4,5,6]
    returns
    [4,5,6]

The pattern behind it

Linked Lists

I reach for it when

  • Nodes point to the next node, and you cannot index into the list.
  • You must reverse, merge, reorder or remove a node.

Say this before you type. Say which pointers you hold and which link you are about to change.

All 12 Linked Lists problems

Two Pointers

I reach for it when

  • The array is sorted, or you can sort it.
  • You need a pair, a triple or a palindrome.

Say this before you type. Say what each pointer means and what a move to the left or right rules out.

All 26 Two Pointers problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Middle of the Linked Listeasy