Remove Nth Node From End

Given the head of a linked list, remove the nᵗʰ node from the end of the list and return its head. Example: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Explanation: The 2nd node from end (value 4) is removed. Pattern focus: Two-pointer gap (use dummy head to handle edge cases).

Input Format

head = ListNode, n = int

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [1, 30].
  • 1 <= n <= length of list
  • 0 <= Node.val <= 100

Examples

Example 1:

Input:

head = [1,2,3,4,5]
n = 2

Output:

[1,2,3,5]

Explanation:

Remove the 2nd node from end (4).

Example 2:

Input:

head = [1]
n = 1

Output:

null

Explanation:

List has one node, remove it to get empty list.

Loading...
Remove Nth Node From End - Linked List