/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode sH = null;
ListNode sT = null;
ListNode bH = null;
ListNode bT = null;
ListNode next = null;
while(head!=null){
//使用next node是为了避免环形链表
next = head.next;
head.next = null;
if(head.val<x){
if(sH==null){
//指向相同的内存地址
sH = head;
sT = head;
}else{
sT.next = head;
sT = head;
}
}else{
if(bH==null){
bH = head;
bT = head;
}else{
bT.next = head;
bT = head;
}
}
head = next;
}
if(sT!=null){
sT.next = bH;
}
return sT!=null ? sH : bH;
}
}