InferenceAI

Enter at least two characters to search.

Start free

Remove Nth From the End

mediumLinked Lists25 min25 XP

Remove the n-th node from the end of the list and return the result.

Linked lists are given and returned as Python lists of node values, from head to tail. Think in terms of nodes and pointers; the list format just makes the tests readable.

Requirements

  • One pass with two pointers nn apart.

Examples

EXAMPLE WALKTHROUGHmiddle

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3,4,5],2]
TASKremove_nth_from_end()
EXPECTED OUTPUT[1,2,3,5]

Start with the input values shown in this example.

Example 1 of 3

Input: values = [1,2,3,4,5], n = 2

Output: [1,2,3,5]

Explanation: middle

Input: values = [1], n = 1

Output: []

Explanation: only node

Input: values = [1,2], n = 1

Output: [1]

Explanation: tail

Hint 1

Move a fast pointer n steps ahead, then move both until fast reaches the end.

Topics

DSALinked ListsPython