24. 两两交换链表中的节点

24. 两两交换链表中的节点
示例:给定 1->2->3->4, 你应该返回 2->1->4->3.
演示图

使用递归算法

class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode temp1 = head;
        ListNode temp2 = head.next;
        
        temp1.next = swapPairs(temp2.next);
        temp2.next = temp1;

        return temp2;
    }
}
原文地址:https://www.cnblogs.com/sweetorangezzz/p/12963679.html