### 题目

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example, Given 1->4->3->2->5->2 and x = 3, return 1->2->2->4->3->5.

### 拆分成ls和gt两个链表

#### 代码

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
ListNode ls = new ListNode(0), gt = new ListNode(0), lsHead = ls, gtHead = gt;
while (cur != null) {
if (cur.val < x) {
ls.next = cur;
ls = ls.next;
} else {
gt.next = cur;
gt = gt.next;
}
cur = cur.next;
}
gt.next = null;
}
}


### 把小于目标值的元素剔除出链表

#### 代码

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
ListNode sentinel = new ListNode(0), cur = sentinel;
ListNode ls = new ListNode(0), lsHead = ls;
while (cur != null && cur.next != null) {
if (cur.next.val < x) { // 发现小点
ls.next = cur.next; // ls链表收编此小点
ls = ls.next;
cur.next = cur.next.next; // 主链表跳过此此小点
} else { // 主链表正常推进
cur = cur.next;
}
}
ls.next = sentinel.next;