Typorithm · easy

Middle of Linked List

middle-of-linked-listlinked-listsfast-slowone-passno-length
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Middle of Linked List

0%

Linked Lists

Run a slow and a fast pointer; when fast reaches the end, slow is at the middle.

The naive way counts the length then walks halfway — two passes.

A fast pointer moving two steps for every one of slow lands slow at the middle in a single pass.

For even lengths this returns the second of the two middle nodes.

01

Fast moves two, slow moves one.

02

Stop when fast hits the end.

03

One pass, no length count.

function middleNode(head) {
  let slow = head;
  let fast = head;

  while (fast !== null && fast.next !== null) {
    slow = slow.next;
    fast = fast.next.next;
  }

  return slow;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Two speeds

Both start at the head; one moves twice as fast.

let slow = head;
  let fast = head

2. Loop guard

fast moves two steps, so both fast and fast.next must exist.

while (fast !== null && fast.next !== null)

3. Fast double-step

When fast reaches the end, slow sits at the middle.

fast = fast.next.next

4. The middle

slow covers half the distance fast covers.

return slow

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Middle of Linked List: Lesson & Practice · Typorithm | ApplyDjinn