public ListNode partition(ListNode head, int x) { ListNode headOfHead = new ListNode(-1); headOfHead.next = head; ListNode p = headOfHead; while (p.next != null) { p = p.next; // CUT begin debug(p.val); // CUT end } ListNode tail = new ListNode(-1); p.next = tail; p = headOfHead; ListNode q = tail; while (p.next != tail) { if (p.next.val < x) { p = p.next; } else { q.next = p.next; q = q.next; p.next = p.next.next; q.next = null; } } p.next = tail.next; return headOfHead.next; }
public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int x = in.nextInt(); ListNode head = new ListNode(-1); ListNode p = head; for (int i = 0; i < n; i++) { p.next = new ListNode(in.nextInt()); p = p.next; // out.println(p.val); } ListNode res = new A1().partition(head.next, x); p = res; while (p != null) { out.println(p.val); p = p.next; } }