MediumLinked ListTwo Pointers

Remove Nth Node From End of List

LeetCode
1 approach, code in all languages

Given the head of a singly linked list and an integer n, delete the node that sits n positions from the end of the list and return the head of the modified list.

The counting is from the tail: n equal to 1 means the last node, n equal to 2 means the second-to-last, and so on. Aim to do this in a single pass over the list.

Example 1

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

Output: [1,2,3,5]

The second node from the end holds value 4, so it is removed.

Example 2

Input: head = [1], n = 1

Output: []

Removing the only node from a one-element list yields an empty list.

Constraints

  • The number of nodes is in the range [1, 30].
  • 1 <= Node.val <= 100
  • 1 <= n <= number of nodes
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now