Delete the First Node of a Linked List
Implement deleteAtBeginning
Given the
head of a singly linked list, delete the first node and return the head of the resulting list. If the list is already empty, return it unchanged.
Since the second node is already fully linked to the rest of the list, deleting the first node is really just a matter of deciding what counts as the head now. The optimal solution never touches, copies, or frees anything — it simply reassigns head to head.next, in O(1).
Example 1:
Input: head = [8,2,3,1,7]
Output: [2,3,1,7]
Example 2:
Input: head = [5]
Output: []
Example 3:
Input: head = []
Output: []
+ 5 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes in head ≤ 10⁴ - ●
-10⁹ ≤ node value ≤ 10⁹ - ●
Deleting from an empty list must return an empty list, not an error
head =
[8, 2, 3, 1, 7]