Swap Nodes in pairs in a linked list - Java solution


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

Log in to reply
 

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