Clear JavaScript O(n) time and O(1) space


  • 0
    var swapPairs = function(head) {
        if (!head || !head.next) return head;
        let one = head;
        head = one.next;
        while (one && one.next) {
            let three = one.next.next;
            one.next.next = one;
            one.next = three && three.next ? three.next : three;
            one = three;
        }
        return head;
    };
    

Log in to reply
 

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