5ms Java Solution:simple and easy to understand


  • 1
    F

    public ListNode swapPairs(ListNode head) {

        if(head != null && head.next != null){
            ListNode temp = head.next.next;
            
            ListNode next = head.next;
            
            next.next = head;
            head.next = this.swapPairs(temp); 
            
            return next;
        }
        return head;
    }

  • 0
    D

    Nice job man.
    Incredible.


Log in to reply
 

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