dsa · easy
Reverse Linked List
You are given the head of a singly linked list. Reverse the list and return the new head.
Arguments
head— the linked list as a ListNode (val,next);Noneif empty
Walk .next on the ListNode. The judge prints the resulting values.
Example
[1,2,3,4,5] → [5,4,3,2,1].
Constraints
0 <= length <= 5000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,3,4,5] Expected: [5,4,3,2,1]
Example 2
Input: [1,2] Expected: [2,1]