转换相邻两个节点的顺序

given: 4->3->2->1 output: 3->4->1->2

并且返回为结果链表的head node。

常规

两个相邻指针,依次向后移动两位进行遍历。

递归

 public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null)
            return head;
        
        ListNode curr = head.next;
        head.next =  curr.next;
        curr.next = head;

        head.next = swapPairs(head.next);
        System.out.println(curr.val);
        return curr;
    }

head : 4->3->2->1

        ListNode curr = head.next;
        head.next =  curr.next;
        curr.next = head;

curr 3->head head 4->2->1

head.next 2->1

swapPairs(head.next);

head.next 1->2

Output : curr 3->4->1->2