class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
//linkedlist at least has two elements.
ListNode next = head.next;
head.next = swapPairs(head.next.next);
next.next = head;
return next;
}
}感觉第24题用递归逻辑会更简洁明了一点。顺便问一下波波老师,这种情况的递归空间复杂度是O(1)还是O(N)? 我觉得是O(1)但是不是很确定