My 5 line Java solution using backtrace


  • 0
    D
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode nextNode = head.next;
            head.next = swapPairs(nextNode.next);
            nextNode.next = head;
            return nextNode;
        }
    }
    

Log in to reply
 

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