C# Simple solution


  • 0
    L
    public ListNode Partition(ListNode head, int x) {
        ListNode dummy = new ListNode(int.MinValue), pre = dummy, cur = dummy;
        dummy.next = head;
        while(cur != null && cur.next != null)
            if(cur.next.val < x)
                if(pre != cur){
                    ListNode tmp = cur.next;
                    cur.next = tmp.next;
                    tmp.next = pre.next;
                    pre.next = tmp;
                    pre = pre.next;
                }
                else pre = cur = cur.next;
            else cur = cur.next;
        return dummy.next;
    }

Log in to reply
 

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