My Java Solution


  • 1
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode newHead = head.next;
        while(head != null && head.next != null) {
            ListNode nextRound = head.next.next;
            head.next.next = head;
            head.next = nextRound == null ? null : (nextRound.next == null ? nextRound : nextRound.next);
            head = nextRound;
        }
        return newHead;
    }

Log in to reply
 

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