Java 5 lines recursive solution


  • 0
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode newhead = head.next;
            head.next = swapPairs(newhead.next);
            newhead.next = head;
            return newhead;
        }
    

Log in to reply
 

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