“RemoveAll”类是链表类的一部分。我编写的类从链表中删除所有键,但不删除重复键。
有人知道为什么吗?我如何也可以删除重复的键?
public class LinkedIntList {
private ListNode front;
private String name = "front";
// Constructs an empty list.
public LinkedIntList() {
front = null;
}
public void removeAll(int key){
if(front == null){
throw new RuntimeException();
}else if( front.data == (key)) {
front = front.next;
return;
}
ListNode cur = front;
ListNode prev = null;
while(cur != null && cur.data != (key) ){
prev = cur;
cur = cur.next;
}
if(cur == null)
throw new RuntimeException();
prev.next = cur.next;
}
最佳答案
如果您有重复项,一种蛮力方法是继续调用 removeAll() 直到它返回 false。
while (myCollection.removeAll(someOthercollection))
; // comment here that the loop does nothing for clarity
添加:正如@Dukeling 正确指出的那样,您不必这样做。但是,如果 removeALL() 实现不当,您可能不得不这样做。
关于java - 领英列表(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193262/