如何在java中更新特定密钥中哈希映射中的节点?
我想从链表中删除所有偶数节点。如何更新hashmap特定键中的节点<代码>H.get(0)。下一步=温度不工作。期望的输出是24,但我没有得到它如何在java中更新特定密钥中哈希映射中的节点?,java,data-structures,linked-list,hashmap,Java,Data Structures,Linked List,Hashmap,我想从链表中删除所有偶数节点。如何更新hashmap特定键中的节点H.get(0)。下一步=温度不工作。期望的输出是24,但我没有得到它 publicstaticvoidmain(字符串[]args){ 头部=新节点(1); head.next=新节点(2); head.next.next=新节点(3); head.next.next.next=新节点(4); head.next.next.next.next=新节点(5); 节点温度=头; HashMapH=新的HashMap(); while
publicstaticvoidmain(字符串[]args){
头部=新节点(1);
head.next=新节点(2);
head.next.next=新节点(3);
head.next.next.next=新节点(4);
head.next.next.next.next=新节点(5);
节点温度=头;
HashMapH=新的HashMap();
while(temp!=null){
如果(临时数据%2==0){
如果(!H.containsKey(0)){
H.put(0,温度);
}否则{
//这种说法行不通
H.get(0)。下一步=温度;
}
}
温度=下一个温度;
}
水头=H.get(0);
while(temp!=null){
系统输出打印(温度数据);
温度=下一个温度;
}
}
}
在链表上执行操作时,通常不需要HashMap,只需保留节点的引用并相应地删除它们即可。
我想这样的办法应该行得通
正如您所看到的,我没有将引用存储在HashMap中,但我尝试从头部到尾部进行探索,保留我探索的前一个节点的引用。为什么?因为当我找到一个偶数节点时,我想将上一个节点连接到下一个节点,这样我就可以删除当前节点。我有偶数节点为头部时的边缘情况,因为没有指定先前的节点
我真的无法尝试这段代码,但可能可以作为您尝试执行的操作的参考
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 current = head;
Node prev = null;
while(current != null){
if(current.data%2==0){
if(current == head){
head = current.next;
} else {
prev.next = current.next;
current = current.next;
}
} else {
prev = current;
current = current.next;
}
}
current = head
while(current!=null){
System.out.print(current.data);
current=current.next;
}
}
在链表上执行操作时,通常不需要HashMap,只需保留节点的引用并相应地删除它们。 我想这样的办法应该行得通 正如您所看到的,我没有将引用存储在HashMap中,但我尝试从头部到尾部进行探索,保留我探索的前一个节点的引用。为什么?因为当我找到一个偶数节点时,我想将上一个节点连接到下一个节点,这样我就可以删除当前节点。我有偶数节点为头部时的边缘情况,因为没有指定先前的节点 我真的无法尝试这段代码,但可能可以作为您尝试执行的操作的参考
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 current = head;
Node prev = null;
while(current != null){
if(current.data%2==0){
if(current == head){
head = current.next;
} else {
prev.next = current.next;
current = current.next;
}
} else {
prev = current;
current = current.next;
}
}
current = head
while(current!=null){
System.out.print(current.data);
current=current.next;
}
}
- 在上一个循环中,您编写了
,但是while(temp!=null){
在循环之后为null,并且不引用头temp
- 我看不到在这里使用
的价值HashMap
Node head;
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 oddTail = null, curr = head;
while (curr != null) {
if (curr.data % 2 != 0) { // Odd data
if(oddTail != null){
oddTail.next = curr; // Update oddTail
}
oddTail = curr;
}
curr = curr.next;
}
if(oddTail != null){ // The oddTail.next might point to a node with even data
oddTail.next = null;
} else{
head = null; // No nodes with odd data
}
curr = head;
// Print the list
while(curr != null){
System.out.println(curr.data);
curr = curr.next;
}
输出:
1
3
5
- 在上一个循环中,您编写了
,但是while(temp!=null){
在循环之后为null,并且不引用头temp
- 我看不到在这里使用
的价值HashMap
Node head;
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 oddTail = null, curr = head;
while (curr != null) {
if (curr.data % 2 != 0) { // Odd data
if(oddTail != null){
oddTail.next = curr; // Update oddTail
}
oddTail = curr;
}
curr = curr.next;
}
if(oddTail != null){ // The oddTail.next might point to a node with even data
oddTail.next = null;
} else{
head = null; // No nodes with odd data
}
curr = head;
// Print the list
while(curr != null){
System.out.println(curr.data);
curr = curr.next;
}
输出:
1
3
5
什么具体不起作用,什么是错误?这里的
HashMap
有什么用?我想在HashMap的特定键中存储节点什么具体不起作用,什么是错误?这里的HashMap
有什么用?我想在HashMap的特定键中存储节点