Delete Node in Linked List

Medium

Description

Given access to a node in a linked list (not the tail), delete it by copying the next node value and deleting next node.

Examples

Input:head = [4,5,1,9], node = 5
Output:[4,1,9]
Explanation:

Delete node with value 5.

Input:head = [2,3,7,8,6], node = 7
Output:[2,3,8,6]
Explanation:

Delete the middle node with value 7. Since only have access to the node itself (not the previous node), copying the next node's value (8) to the current node and then skip the next node.

Input:head = [10,20], node = 10
Output:[20]
Explanation:

Delete the first node with value 10 from a minimal 2-node list. Copying the next node's value (20) into the current node and remove the next node, effectively deleting the original first node.

Constraints

  • 2 ≤ nodes ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!