CodePark/ problems / Remove the Nth Node from the End
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Linked lists

Remove the Nth Node from the End

mediumabout 20 minutes in an interview

Remove the n-th node counting from the end of a singly linked list. Return the head of the changed list.

You get the head of the list and a number n. n = 1 means the last node.

  • The list has at least one node, and 1 <= n <= length.
  • If you remove the head, return the new first node. If the list is now empty, return null.
  • Reuse the existing nodes.
  • Try to do it in one pass over the list.
Input: head = 10 → 20 → 30 → 40 → 50, n = 2 Output: 10 → 20 → 30 → 50Count from the end: 50 is 1st and 40 is 2nd.
Input: head = 10 → 20, n = 2 Output: 20The 2nd node from the end is the head itself.
Input: head = 10, n = 1 Output: null