315. Remove Nth Node from End of List

EasyLinked ListLinked ListTwo PointerDeletion

Given the head of a singly linked list and an integer n, remove the nth node from the end of the list and return its head.

n is always valid. Use one-pass two-pointer technique.

Input: Head of a singly linked list and integer n.

Output: Head of the updated linked list.

Examples

Example 1
Input: [1,2,3,4,5], 2
Output: [1,2,3,5]
Explanation: 2nd from end is node(4). Remove it. Result: 1→2→3→5.
Example 2
Input: [1,2,3,4,5], 5
Output: [2,3,4,5]
Explanation: 5th from end = head(1). Remove head. Return node(2).
Example 3
Input: [1], 1
Output: []
Explanation: Only node removed. Return NULL.

Constraints

Asked by

MetaAccentureMicrosoftAmazonBloombergApple
Solve this problem in the editor →