Solution 1: accepted 1ms
Create a new node, looks not good.
|
|
Solution 2: accepted 0ms
No new nodes created yet still complicated.
|
|
Solution 3: accepted 0ms
Should be the most concise iterative solution.
Time: O(n)
Space: O(1)
|
|
Solution 4: accepted 1ms
Recursive solution.
- Base case: when
head == null || head.next == null
, we have the newHead, which is the last element in LinkedList. - Recursive rule: next.next = current; current.next = null.
Time: O(n)
Space: O(n) (stack space consumption)
|
|