我正在學習解決複雜的算法。爲此我遇到了LinkedList的實現。我想了解上述解決方案。在appendToTail中,我不明白while循環和while循環之後的行。在deleteNode中,我看不到節點被刪除的地方。LinkedList - 試圖瞭解實現
class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
Node end = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) {
return head.next; /* moved head */
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; /* head didn’t change */
}
n = n.next;
}
}
}
感謝@Leon。我現在得到了 –