Recursive Java solution


  • 0
    A

    public class Solution {
    public ListNode swapPairs(ListNode head) {
    if(head==null)
    {
    return head;
    }
    if(head.next==null)
    {
    return head;
    }
    ListNode first=head,second=head.next,temp;
    temp=second.next;
    second.next=first;
    first.next=swapPairs(temp);
    return second;
    }
    }


Log in to reply
 

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