Leetcode Problem 19. Remove Nth Node From End of List

19. Remove Nth Node From End of List

Leetcode Solutions

One Pass Algorithm to Remove Nth Node From End of List

  1. Initialize two pointers slow and fast to the dummy node, which points to the head of the list.
  2. Advance the fast pointer n+1 steps ahead.
  3. Move both slow and fast pointers one step at a time until fast reaches the end of the list.
  4. The slow pointer will now be just before the node to remove.
  5. Change the next pointer of the slow pointer to skip the node to be removed.
  6. Return the next of the dummy node, which is the new head of the list.
UML Thumbnail

Two Pass Algorithm to Remove Nth Node From End of List

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...