Very Simple Java Recursive Solution


  • 2
    C
    public class Solution {
        public ListNode swapPairs(ListNode head) {
        ListNode newhead = null;
            if(head!= null && head.next!=null)
            {
                ListNode result = swapPairs(head.next.next);
                newhead = head.next;
                newhead.next = head;
                head.next = result;
                return newhead;
            }
            return head;
        }
    }

Log in to reply
 

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