【题目】
与上一篇博客相似,唯一的不同就是在原问题的要求智商再增加如下两个要求。
在左、中、右三个部分的内部也做顺序要求,要求每部分里的节点从左到右的顺序与原链表中节点的先后次序一致
【代码】
public static Node listPartition2(Node head,int pivot) {
Node sH = null;
Node sT = null;
Node eH = null;
Node eT = null;
Node bH = null;
Node bT = null;
Node next =null;
while(head!=null) {
next = head.next;
head.next = null;
if(head.value < pivot) {
if(sH==null) {
sH = head;
sT = head;
}else {
sT.next = head;
sT = head;
}
}else if(head.value == pivot) {
if(eH==null) {
eH = head;
eT = head;
}else {
eT.next = head;
eT = head;
}
}else {
if(bH==null) {
bH = head;
bT = head;
}else {
bT.next = head;
bT = head;
}
}
head = next;
}
if(sT != null) {
sT.next = eH;
eT = eT == null ? sT : eT;
}
if(eT!=null) {
eT.next = bH;
}
return sH != null ? sH : eH !=null ? eH : bH;
}