Problem 235396 · easy · Phase 02 Linear Data Structures

Delete the Node at a Position

linked list · deletion · indexing

Given the head of a singly linked list and a 0-based index, remove the node at that position and return the head of the resulting list. If index is negative or there is no node at that position, return the list unchanged.

Examples

Input:  head = 1 -> 2 -> 3, index = 1
Output: 1 -> 3

Input:  head = 1 -> 2 -> 3, index = 0
Output: 2 -> 3

Input:  head = 1 -> 2 -> 3, index = 3
Output: 1 -> 2 -> 3

Constraints

  • 0 <= number of nodes <= 10**4
  • -10**4 <= index <= 10**4
  • Target: O(index) time, O(1) extra space

Goals

  • Locate the predecessor of a position
  • Leave the list untouched for out-of-range positions
Starting Python…