AC JAVA solution

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

  • -3

    recursion is not necessary and will add stack consumption

  • 0

    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.