public ListNode partition(ListNode head, int x) {

    if (head == null) {
      return null;
    }

    ListNode dummy = new ListNode(0);
    dummy.next = head;

    ListNode curt = head;
    ListNode nodePrev = dummy;
    while (curt != null && curt.val < x) {
      nodePrev = curt;
      curt = curt.next;
    }

    ListNode node = curt;
    ListNode curtPrev = curt;
    // curt = curt.next;

    while (curt != null) {
      if (curt.val < x) {
        curtPrev.next = curt.next;
        curt.next = node;
        nodePrev.next = curt;
        nodePrev = curt; // missing for the first time

        curt = curtPrev.next;
      } else {
        curtPrev = curt;
        curt = curt.next;
      }
    }

    return dummy.next;
  }