Skip to content
BASA DSA

Remove Nth Node From End of List

MediumLinked List

Remove the nth node counted from the end of an acyclic list and return the resulting head.

Example 1

Argument 1: next pointers; tail points to null.Argument 1: next pointers; tail points to null. Vertices: node 0: 5, node 1: 6, node 2: 7, node 3: 8. Edges: node 0 (5) → node 1 (6); node 1 (6) → node 2 (7); node 2 (7) → node 3 (8).5678
Argument 1: next pointers; tail points to null.

Input
head = [5,6,7,8]
n = 2
Output
[5,6,8]

Explanation

Counting backwards gives 8 first and 7 second; remove 7 and join 6 to 8.

Example 2

Argument 1: next pointers; tail points to null.Argument 1: next pointers; tail points to null. Vertices: node 0: 9. Edges: none.9
Argument 1: next pointers; tail points to null.

Input
head = [9]
n = 1
Output
null

Explanation

Removing the only node leaves an empty list.

Example 3

Argument 1: next pointers; tail points to null.Argument 1: next pointers; tail points to null. Vertices: node 0: 1, node 1: 2. Edges: node 0 (1) → node 1 (2).12
Argument 1: next pointers; tail points to null.

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

Explanation

The second node from the end is head 1, so head becomes 2.

Constraints

  • 1 <= n <= number of nodes <= 10000

Function contractremoveNthFromEndArguments: list, json
Loading draft…
SolutionRuns on your device
Loading draft…