“RemoveAll”类是 Linked List 类的一部分。我编写的类从链接列表中删除所有键,但不会删除重复键。
有人知道为什么吗?我如何也可以删除重复的键?
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;
}