My iterative java solution


  • 0
    A
    static ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        ListNode root = head;
        while (head.next != null && head.next.next != null) {
            dummy = head.next;
            head.next = head.next.next;
            dummy.next = head.next.next;
            head.next.next = dummy;
            head = dummy;
        }
        return root.next;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.