Leetcode Problem 2095. Delete the Middle Node of a Linked List

2095. Delete the Middle Node of a Linked List

Leetcode Solutions

Fast and Slow Pointers Approach

  1. Check if the list is empty or has only one node. If so, return null or None.
  2. Initialize two pointers, slow pointing to head and fast pointing to head.next.next.
  3. Traverse the list while fast is not null and fast.next is not null: a. Move fast forward by two nodes. b. Move slow forward by one node.
  4. After the loop, slow will be pointing to the node before the middle node. Delete the middle node by setting slow.next to slow.next.next.
  5. Return the head of the modified list.
UML Thumbnail

Two Passes Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...