AC JAVA solution


  • 5
    W
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        
        ListNode n1 = head;
        ListNode n2 = head.next;
        
        n1.next = n2.next;
        n2.next = n1;
        
        n1.next = swapPairs(n1.next);
        
        return n2;
    }

  • -3
    F

    recursion is not necessary and will add stack consumption


  • 0
    B

    As the question says, your algorithm does not use only constant space.


Log in to reply
 

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