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; }
`
添加回答
举报