Given the head of a singly linked list, reverse the list, and return the reversed list. Do it in-place with O(1) extra space.
head = [1,2,3,4,5][5,4,3,2,1]head = [1,2][2,1]prev = null, curr = headcurr != nullnxt = curr.next (don't lose the rest of the list)curr.next = prevprev = curr, curr = nxtprev — the new head of the reversed list
At every step, everything behind curr is already a valid reversed sub-list whose new head is prev.
By saving nxt before clobbering curr.next, we never lose the forward chain.
When curr reaches null, the entire list has been reversed and prev points to the new head.