My 0ms java solution


  • 4
    X
    public ListNode swapPairs(ListNode head) {
    	if (head == null || head.next == null)
    		return head;
    	ListNode dummy = new ListNode(0);
    	ListNode temp = dummy;
    	while (head != null && head.next != null) {
    		temp.next = head.next;
    		head.next = head.next.next;
    		temp.next.next = head;
    		temp = head;
    		head = head.next;
    	}
    	return dummy.next;
    }

Log in to reply
 

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