代码随想录
class Solution {// 举例子:假设两个节点 1 -> 2// 那么 head = 1; next = 2; next.next = null// 那么swapPairs(next.next),传入的是null,再下一次递归中直接返回null// 因此 newNode = null// 所以 next.next = head; => 2.next = 1; 2 -> 1// head.next= newNode; => 1.next = null; 1->null// 所以 2->1->null// 最终返回 next,即返回 2public ListNode swapPairs(ListNode head) {if(head == null || head.next == null)return head;ListNode next = head.next;ListNode newNode = swapPairs(next.next);next.next = head;head.next = newNode;return next;}}