Share my 0ms java solution and easy to understand


  • 0
    C
    public ListNode partition(ListNode head, int x) {
        ListNode lessHead = new ListNode(0), lessRear = lessHead;
        ListNode greaterHead = new ListNode(0), greaterRear = greaterHead;
        for(ListNode p = head; p!=null; p=p.next){
            if(p.val < x){
                lessRear.next = p;
                lessRear = p;
            }
            else{
            	greaterRear.next = p;
            	greaterRear = p;
            }
        }
        lessRear.next = greaterHead.next;
        greaterRear.next = null;
        return lessHead.next;
    }

Log in to reply
 

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