Remove Nth Node From End of List

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

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

Note: You can assume n always points to a real node in the list. In other words, 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]

Remember to understand the problem and plan your approach before you start coding.