303. Delete a Node by Value

EasyLinked ListLinked ListDeletion

Given the head of a singly linked list and an integer val, delete the first occurrence of a node with value val from the list and return the head.

If the value does not exist, return the list unchanged.

Input: Head of a singly linked list and an integer val.

Output: Head of the updated linked list.

Examples

Example 1
Input: [1,2,3,4,5], 3
Output: [1,2,4,5]
Explanation: Find node(3). Its previous node(2) points to node(4). Node(3) is unlinked.
Example 2
Input: [1,2,3], 1
Output: [2,3]
Explanation: Delete the head node. Return node(2) as new head.
Example 3
Input: [1,2,3], 4
Output: [1,2,3]
Explanation: Value 4 not found. Return list unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →