java simple solution, easy to understand.time complex is o(n) . 1ms spend


  • 0
    S
     public ListNode partition(ListNode head, int x) {
            ListNode small,large;
            small = new ListNode(0);
            large = new ListNode(0);
            ListNode smallTmp = small;
            ListNode largeTmp = large;
            while (head != null){
                if (head.val >= x){
                    largeTmp.next = head;
                    largeTmp = largeTmp.next;
                }else{
                    smallTmp.next = head;
                    smallTmp = smallTmp.next;
                }
                head = head.next;
            }
            smallTmp.next = large.next;
            largeTmp.next = null;
            return small.next;
        }
    

Log in to reply
 

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