Simple 6-line Recursive Java Solution


  • 0
    N
    public class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode temp = head.next;
        head.next = temp.next;
        temp.next = head;
        if(head.next != null && head.next.next != null) head.next = swapPairs(head.next);
        return temp;
    }
    

    }


Log in to reply
 

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