9-Lines Very Neat Java Solution


  • 0
    O
    public ListNode swapPairs(ListNode head) {
            ListNode dmy = new ListNode(0);
            dmy.next = head;
            for (ListNode i = dmy; i.next != null && i.next.next != null; ) {
                ListNode odd = i.next, even = odd.next;
                odd.next = even.next;
                i.next = even;
                even.next = odd;
                i = odd;
            }
            return dmy.next;
        }
    

Log in to reply
 

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