Iterative JS solution


  • 0
    R
    var swapPairs = function(head) {
        if(head === null || head.next === null)
            return head;
        start = head.next;
        prev = null;
        while(head !== null && head.next !== null) {
            if(prev !== null)
                prev.next = head.next;
            temp = head.next.next; 
            head.next.next = head;
            head.next = temp; 
            prev = head;
            head = temp; 
        }
            return start;
    };

Log in to reply
 

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