Easy to understand recursive solution


  • 3
    T
    public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode first = head;
            ListNode second = head.next;
            ListNode third = second.next;
            
            first.next = swapPairs(third);
            second.next = first;
            return second;
        }

  • 0
    J

    Very Clean, thanks.


Log in to reply
 

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