967. Insertion before the value X in Linked List
Given the head of a singly linked list and two integers X and val.
Insert a node with value val before the node with value X in the linked list and return the head of the modified list.
Example 1:
Input: linkedList = [1, 2, 3], X = 2, val = 5
Output: [1, 5, 2, 3]
Explanation:
The node with value 5 was added before the node with value 2
Example 2:
Input: linkedList = [1, 2, 3], X = 7, val = 5
Output: [1, 2, 3]
Explanation:
No node was added as X was not found in the list.
Now Your Turn!
Pick the correct output for the given inputInput: linkedList = [1], X = 1, val = 10
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == number of nodes in the Linked List
- 1 <= n <= 1000
- 0 <= ListNode.val <= 100
- 0 <= X <= 100
- 0 <= val <= 100
- Number of nodes with value X is 0 or 1