Delete a Node Without Head
EasyP2Coding
You are given a singly linked list and the 0-based index of a node target that is not the last node. Delete target from the list while using only target itself: you may not use the head and you may not use the node before it.
The judge builds the list from an array, so the function receives head and index. Use head only to walk to target, then treat target as the only handle you have. Return the head of the list. A list node has val and next. The ListNode class is already defined for you.
Example 1
- Input
- head = [4,5,1,9], index = 1
- Output
- [4,1,9]
Example 2
- Input
- head = [1,2,3], index = 0
- Output
- [2,3]
Example 3
- Input
- head = [7,8], index = 0
- Output
- [8]
Constraints
- 2 <= number of nodes <= 1000
- 0 <= index <= number of nodes - 2
- -10^4 <= Node.val <= 10^4
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.