Java recursive solution


  • 0
    M
    public ListNode partition(ListNode head, int x) {
        if(head == null || head.next == null) return head;
        ListNode currp = head, nextp = partition(head.next, x);
        currp.next = nextp;
        if(currp.val >= x){
            if(nextp != null && nextp.val < x){
                head = nextp;
                while(nextp.next != null && nextp.next.val < x){
                    nextp = nextp.next;
                }
                currp.next = nextp.next;
                nextp.next = currp;
            }
        }
        return head;
    }

Log in to reply
 

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