My simple recursive Java solution


  • 0
    Y
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        else {
            ListNode newHead = head.next;
            head.next = swapPairs(newHead.next); //recursively call swapPairs function to reverse two paired Nodes
            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.