Remove Nth Node From End of List

Medium

Extra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on the hints if you get stuck.

Question

Given the head of a linked list and an integer n, remove the node that sits n positions from the end of the list, then return the head of the resulting list.

Note: You can assume n always lands on a real node in the list, so 1 <= n <= length of the list.

Input: head = [1 -> 2 -> 3 -> 4 -> 5], n = 2

Output: [1 -> 2 -> 3 -> 5]

Input: head = [1], n = 1

Output: []

Input: head = [1 -> 2], n = 2

Output: [2]

Follow up: Could you solve this by walking the list only once?

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

Which list results from removing the node 3 positions from the end of head = [3 -> 6 -> 9 -> 12]?
[3 -> 6 -> 9]
[3 -> 9 -> 12]
[6 -> 9 -> 12]
[3 -> 6 -> 12]

Take a moment to understand the problem and think of your approach before you start coding.