Share my accepted Java solution


  • 10
    I
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode first = head, last = head.next;
            first.next = swapPairs(last.next);
            last.next = first;
            return last;
        }
    }

Log in to reply
 

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