LeetCode: Delete the Middle Node of a Linked List Solution

Fast and slow

Approach

Fast and slow but fast node will make a move first

Implementation

1var deleteMiddle = function (head) {
2 if (!head.next) return null
3
4 for (
5 var slow = head, fast = head.next.next;
6 fast && fast.next;
7 slow = slow.next, fast = fast.next.next
8 ) {}
9 slow.next = slow.next.next
10
11 return head
12}

References

Original problem

Similar problems

Remove Nth Node From End of List

Reorder List

Remove Linked List Elements

Middle of the Linked List

Comments

Loading comments...

Tags

leetcode

linked list

two pointers

Next Post

LeetCode: Split Linked List in Parts

Sep 24, 2022

Naming is hard

Previous Post

LeetCode: Spiral Matrix II

Sep 20, 2022

Same with I, with a small modification

HoningJS

Search Posts