head of a singly linked list of integers. Sort the list in non-decreasing order and return its new head.
Do it the way you sort a hand of cards: take the nodes one at a time from left to right and insert each into its place in the already sorted part. There is no need to create new nodes; rewiring the next links of the existing ones is enough.head=list_node([7, 3, 9, 3]) → [3, 3, 7, 9]
Both 3s move in front of 7, and 9 stays at the end.
head=list_node([-2, 0, 6]) → [-2, 0, 6]
The list is already sorted, so each node simply stays at the tail.
head=list_node([42]) → [42]
Node.val ≤ 5000head=list_node([7, 3, 9, 3])
[3, 3, 7, 9]
Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.