My recursive Java solution (easy, short, readable)


  • 0
    P

    public ListNode swapPairs(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode p = swapPairs(head.next.next);
    head.next.next = head;
    ListNode res = head.next;
    head.next = p;
    return res;
    }


Log in to reply
 

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