Java, Non-recursive, 11 lines, 0ms, simple


  • 1
    public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode h = new ListNode(0);
        h.next = head;
        ListNode t = h;
        while (t.next!=null && t.next.next!=null) {
            ListNode t2 = t.next.next;
            t.next.next = t2.next;
            t2.next = t.next;
            t.next = t2;
            t = t2.next;
        }
        return h.next;
    }
    

    }


Log in to reply
 

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