Problem · Linked List
Delete a Linked-List Node Without the Head
Learn this problemProblem statement
You are given only a node node from a singly linked list. You are not given the list's head. Delete node from the list in place.
The provided node is guaranteed not to be the tail. Copy the successor's value into node, bypass that successor, and return node. The returned list therefore represents the suffix beginning at the original target position after deletion.
Function
deleteNode(node: ListNode) → ListNodeExamples
Example 1
node = [5,1,9]return = [1,9]The target contains 5. Copy 1 from its successor into the target, then bypass that successor.
Example 2
node = [7,3]return = [3]The target takes its successor's value 3 and becomes the last node.
Constraints
- The provided
nodeis notnull. - The provided
nodeis not the tail node. - The serialized suffix contains between
2and10^5nodes. - Each node value fits in a 32-bit signed integer.