Skip to main content
All Questions

Given a head node of a linked list, delete k-th node from the end of the list.

Medium
Unlock detailed company stats for this questionUpgrade

Given the head of a linked list, remove the nth node from the end of the list and return the head of the result.

Example 1

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

The second node from the end holds 4.

Example 2

Input: head = [1], n = 1 Output: []

Removing the only node leaves an empty list.

Constraints

  • The list has between 1 and 30 nodes.
  • 0 <= Node.val <= 100
  • 1 <= n <= the number of nodes in the list.

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.