Linked List· 25 min

Delete the Middle Node of a Linked List

LeetCode #2095 · Medium · Interactive step-by-step visualization

Setup

Example inputhead = [1, 3, 4, 7, 1, 2, 6]
Step 1 / 8

Current step

1 / 8

Find the middle in one pass. A singly linked list has no length and no random access, so counting first would need two passes. Advance a slow pointer one node and a fast pointer two nodes at a time.

Step log

head

head
1
3
4
7
1
2
6
null

list

1 → 3 → 4 → 7 → 1 → 2 → 6

Named pointer
Active node
Committed or completed
Detached or removed
delete-the-middle-node-of-a-linked-list.java
1public ListNode deleteMiddle(ListNode head) {
2 if (head.next == null) return null;
3 ListNode prev = null, slow = head, fast = head;
4 while (fast != null && fast.next != null) {
5 prev = slow;
6 slow = slow.next;
7 fast = fast.next.next;
8 }
9 prev.next = slow.next;
10 return head;
11}
● Java
Time
O(n)
Space
O(1)
Pattern
linked list
Frames
8