AlgoLounge

Remove Nth Node From End of List

medium

You are given the beginning of a linked list head, and an integer n.

Remove the nth node from the end of the list and return the beginning of the list.

Examples

Example 1

Input:  head = [1,2,3,4], n = 2
Output: [1,2,4]

Example 2

Input:  head = [5], n = 1
Output: []

Example 3

Input:  head = [1,2], n = 2
Output: [2]

Constraints

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

How the tests work: lists are written as arrays — [1,2,3] is the chain 1 → 2 → 3. Your function receives real ListNode objects, and the list you return is read back into an array.

TypeScript
Loading editor…
List of test cases (5)
Case 1
head = [1,2,3,4]
n = 2
Case 2
head = [5]
n = 1
Case 3
head = [1,2]
n = 2
Case 4
head = [1,2,3,4,5]
n = 1
Case 5
head = [1,2,3,4,5]
n = 5