Remove Nth Node From End

Medium

Remove Nth Node From End

Problem Statement

Given the head of a singly linked list and an integer

n
, remove the node at the
n
-th position from the end of the list.

Rules and Constraints

  • The linked list is 1-indexed.
  • The size of the list is
    N
    , and
    n
    is guaranteed to be within the valid range
    [1, N]
    .
  • The list does not contain duplicates and nodes do not have a value that is the same as
    n
    .
  • It is not allowed to access the list in a reverse direction.
  • The time complexity of the solution should be O(L), where
    L
    is the length of the list.
  • The space complexity of the solution should be O(1).

Note that the problem is asking to remove the node at the

n
-th position from the end of the list, not from the beginning.

Example

Input: {"head":[1,2,3,4,5],"n":2} Output: [1,2,3,5]

CompaniesMicrosoft
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.