34. Remove Nth Node From End of List

34. Remove Nth Node From End of List

Medium
49.0%
1.8k
linked-listtwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale backend system maintains ordered streams of data using singly linked lists. In many workflows such as log cleanup, rolling window processing, or memory optimization, the system must remove an element relative to the end of the list, not the beginning.

Because the size of the list may not be known in advance, the platform needs an efficient way to remove the n-th node from the end in a single pass, without converting the list into another data structure.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given the head of a singly linked list and an integer n, remove the n-th node from the end of the list and return the head of the modified list.

You must remove exactly one node.

Input Format

  • head: the head node of a singly linked list
  • n: an integer representing the position from the end

Each node is defined as:

ListNode { int val; ListNode next; }

Output Format

  • Return the head of the linked list after removal

Examples

Example 1:

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

Example 2:

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

Example 3:

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

Constraints

  • 1 <= number of nodes <= 5,000
  • 1 <= n <= number of nodes
  • -10^9 <= Node.val <= 10^9

Loading editor...

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

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback