Delete a node from a singly linked list when only that node is given.
Problem
You are given a reference to a node in a singly linked list. The node to delete is not the tail of the list.
Remove that node from the list so that the linked list no longer contains it, and the relative order of the remaining nodes stays the same.
You do not have access to the head of the list.
Important constraint
Because the head is unavailable, you cannot simply search for the previous node. Instead, you must modify the list in place using only the given node.
Goal
Update the linked list so that the given node is logically deleted from the chain.
Input Format
- A reference to a node in a singly linked list.
- The node is guaranteed to be non-tail.
- The linked list nodes contain integer values.
Output Format
Return nothing. Modify the linked list in place by deleting the given node.
Constraints
- The given node is guaranteed to have a next node.
- The list is singly linked.
- You may not access the head node.
- The node to delete is guaranteed not to be the tail.
Example 1
Input
head = [4,5,1,9], node = 5
Output
head = [4,1,9]
Explanation
Copy the value from the next node into the given node, then bypass the next node. The list becomes 4 -> 1 -> 9.
Example 2
Input
head = [1,2,3,4], node = 3
Output
head = [1,2,4]
Explanation
The node with value 3 is replaced in place by its next node's value, and the next node is removed from the chain.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.