为什么值sH,eH和bH改变了?
因为这三个值只使用一次。我真的不明白这个问题。
public class Problem_08_SmallerEqualBigger {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node listPartition2(Node head, int pivot) {
Node sH = null; // Small head
Node sT = null; // Small tail
Node eH = null; // Equal head
Node eT = null; // Equal tail
Node bH = null; // Big head
Node bT = null; // Big tail
Node next = null; // Save next node
// Every node distributed to three lists
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;
}
// 1 2 3 4 5 6
} else if (head.value == pivot) {
if (eH == null) {
eH = head; //1
eT = head; //1
} else {
eT.next = head; // 1 ->2 2->3
eT = head;
}
} else {
if (bH == null) {
bH = head;
bT = head;
} else {
bT.next = head;
bT = head;
}
}
head = next;
}
// Small and equal reconnect
if (sT != null) {
sT.next = eH;
eT = eT == null ? sT : eT;
}
// All reconnect
if (eT != null) {
eT.next = bH;
}
return sH != null ? sH : eH != null ? eH : bH;
}
}