LeetCode 19. Remove Nth Node From End of List

Description

https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

Given a linked list, remove the nth node from the end of list and return its head.

For example,

Note:
Given n will always be valid.
Try to do this in one pass.

Explanation

We introduce a preDelete pointer first and place it at the beginning of the linked list.

  1. Move head pointer first so that the distance between preDelete and head pointer would be N nodes.
  2. Move head and preDelete pointer together until head is reaching to the end.
  3. Modify preDelete pointing node.next to the next node of the delete node.

Video Tutorial


 

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *