 All Problems
Delete Node in a Linked List
medium
linked list
amazon
microsoft
facebook

There is a singly-linked list. You are given access to a node to be deleted (not the tail). Your task is to delete that node.

Note: You are NOT given access to the head of the list. You are only given access to the node to delete.

The technique: copy the next node's value into this node and bypass the next node.

Example 1:

Input:
4 5 1 9
5
Output: 4 1 9

(Delete node with value 5; given only that node)

Example 2:

Input:
4 5 1 9
1
Output: 4 5 9

Constraints:

  • The number of nodes in the list is in the range [2, 1000]
  • The node to delete is not the tail

Input format: First line: list values. Second line: value of node to delete.

Output format: Resulting list.

Run to check your code against the sample cases, or submit to run every case