Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. The linked list will have unique values. You are given the node to delete, and you should delete it in-place. Example: Input: head = [4,5,1,9], node = 5 (the node to delete) Operation: Delete the node with value 5. After deletion: [4,1,9] Explanation: Copy the next node's value to the given node and bypass it. Pattern focus: Pointer Rewiring.

Input Format

node = ListNode

Output Format

return void

Constraints

  • The number of nodes is in the range [2, 1000].
  • Each node has a unique value.
  • The given node will not be the tail and it will always be valid.

Examples

Example 1:

Input:

head = [4,5,1,9]
nodeVal = 5

Output:

[4,1,9]

Explanation:

Deleted node 5 by copying next node 1's data.

Loading...
Delete Node in a Linked List - Linked List