clean-java-solution


  • 0
    N
     public ListNode partition(ListNode head, int x) {
            if (head == null || head.next == null) {
    			return head;
    		}
    
    		ListNode dummy1 = new ListNode(0);
    		ListNode dummy2 = new ListNode(0);
    
    		dummy1.next = head;
    		ListNode traverse1 = dummy1;
    		ListNode traverse2 = dummy2;
    
    		while (traverse1.next != null) {
    			if (traverse1.next.val >= x) {
    				traverse2.next = new ListNode(traverse1.next.val);
    				traverse2 = traverse2.next;
                    traverse1.next = traverse1.next.next;
    			} else {
    				traverse1 = traverse1.next;
    			}
    		}
    
    		traverse1.next = dummy2.next;
    
    		return dummy1.next;
        }
    

Log in to reply
 

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