3 回答

TA貢獻1827條經驗 獲得超4個贊
這里的問題是
if (!duplicate) {
duplicate = true;
currentNode = currentNode.next;
}
您正在將replicate = true標記為true并立即分配“currentNode = currentNode.next;”,由于此引用正在保留下一個節點,因此
1. Put the condition outside of the loop to check whether the head element itself is
that node, if->yes mark isDuplicate = true and proceed in the loop.
2. Inside the loop check afterward and then assign the next node.
希望這應該有效

TA貢獻1963條經驗 獲得超6個贊
您跳過了頭節點。嘗試替換
Node currentNode = head;
跟
Node currentNode = new Node(); currentNode.next = head;

TA貢獻1856條經驗 獲得超11個贊
刪除節點后,應更新當前節點引用以及頭>下一個節點應指向當前節點。請嘗試以下代碼:
if (!duplicate) {
duplicate = true;
currentNode = currentNode.next;
head.next= currentNode.next;
}else {
currentNode.next = currentNode.next.next;
currentNode = currentNode.next;
head.next = currentNode; }
`
添加回答
舉報