【发布时间】:2021-08-12 03:11:55
【问题描述】:
我想从链表中删除所有偶数节点。如何更新 hashmap 的特定键中的节点? H.get(0).next=temp 不工作。所需的输出是 2 4,但我没有得到它。
public static void main(String[] args) {
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
Node temp=head;
HashMap<Integer,Node>H=new HashMap<>();
while(temp!=null) {
if(temp.data%2==0) {
if(!H.containsKey(0)) {
H.put(0, temp);
} else {
//This statement is not working
H.get(0).next=temp;
}
}
temp=temp.next;
}
head=H.get(0);
while(temp!=null) {
System.out.print(temp.data);
temp=temp.next;
}
}
}
【问题讨论】:
-
究竟是什么不起作用,错误是什么?
-
这里
HashMap有什么用? -
我想把节点存储在hashmap的特定key中
标签: java data-structures linked-list hashmap