Share my Java accepted code 409ms.

  • 0
    public ListNode partition(ListNode head, int x) {
        if(head == null) return null;
        if( return head;
        ListNode dummy=new ListNode(0);
        ListNode dummy2=new ListNode(0);;
        ListNode node1 = dummy;  //used to connect smaller #
        ListNode node2 = dummy2; //used to connect greater or equal #
        while( !=null)
            if( < x) //when it's smaller connect to node1
                node1 =; 
            else   //when it's bigger or equal, connect to node2
       =; //connect it to node2
                node2 =; //node2 move to next one
       =; //node1 deletes the next node(
        }; //don't forget this. it is the last node in the link =; //connect the two parts together

Log in to reply

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