// 使用额外空间:把链表节点放入数组中,做分区操作类似快排划分,然后再串起来。
public static Node ListPartition1(Node head , int privot){
Node temp = head;
int count = 0;
while(temp != null){
count++;
temp = temp.next;
}
// 构建与node节点等量数组
Node [] list =new Node [count];
temp = head;
// 将链表数拷入数组
for (int i = 0; i < list.length; i++) {
list[i] = temp;
temp = temp.next;
}
// 得到划分完值的
partition(list,privot);
int i=0;
for ( i = 1; i < list.length; i++) {
list[i-1].next =list[i];
}
// 不加上会知一直循环
System.out.println(i);
list[i-1].next = null;
return list[0];
}
public static void partition(Node [] arr , int privot){
int less = -1;
int more = arr.length;
int index = 0;
while(index < more){
if(arr[index].value < privot){
Swap(arr, ++less ,index++);
}else if(arr[index].value > privot){
Swap(arr, --more, index);
}else{
index++;
}
}
}
public static void Swap(Node [] arr,int i,int j){
Node temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// 不使用额外空间:使用指针来做
// 六个变量,小于部分头尾 SH=null;ST=null; 等于部分头尾EH=null;ET=null;
// 大于部分头尾BH=null;,BT=null;
// 最开始找到的对应的节点头尾都指向,后面再找到的节点改变尾巴并和头串起来。
// 最后小于区域的尾巴连等于区域的头,等于区域的尾巴连大于区域的头。
public static Node Listpartition2(Node head , int privot){
Node sH = null; // small head
Node sT = null; // small tail
Node eH = null; // equal head
Node eT = null; // equal tail
Node mH = null; // big head
Node mT = null; // big tail
Node next = null; // save next node
while (head != null) {
next = head.next;
head.next = null;
if (head.value < privot) {
if (sH == null) { // 第一次找到比privot小的
sH = head;
sT = head;
} else {
sT.next = head; // 不是第一次了,尾巴后驱指向新来的小于pivot节点
sT = head; // 尾巴指向新来的
}
} else if (head.value == privot) {
if (eH == null) {
eH = head;
eT = head;
} else {
eT.next = head;
eT = head;
}
} else {
if (mH == null) {
mH = head;
mT = head;
} else {
mT.next = head;
mT = head;
}
}
head = next;
}
// 小于区域的尾巴,连等于区域的头,等于区域的尾巴连大于区域的头
if (sT != null) { // 如果有小于区域
sT.next = eH;
eT = eT == null ? sT : eT; // 下一步,谁去连大于区域的头,谁就变成eT
}
// 下一步,一定是需要用eT 去接 大于区域的头
// 有等于区域,eT -> 等于区域的尾结点
// 无等于区域,eT -> 小于区域的尾结点
// eT 尽量不为空的尾巴节点
if (eT != null) { // 如果小于区域和等于区域,不是都没有
eT.next = mH;
}
return sH != null ? sH : (eH != null ? eH : mH);// 最后判断返回哪个区域的head
}