 All Problems
Remove Nth Node From End of List
medium
linked list
two pointers
amazon
google
microsoft

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

Example 1:

Input:
1 2 3 4 5
2
Output: 1 2 3 5

Example 2:

Input:
1
1
Output: (empty)

Example 3:

Input:
1 2
1
Output: 1

Constraints:

  • The number of nodes in the list is sz
  • 1 ≤ sz ≤ 30
  • 0 ≤ Node.val ≤ 100
  • 1 ≤ n ≤ sz

Follow-up: Could you do this in one pass?

Input format: First line: space-separated integers for the linked list. Second line: integer n.

Output format: Space-separated integers of the resulting list, or empty line if the list becomes empty.

Run to check your code against the sample cases, or submit to run every case