如何更新 java 中特定键的散列映射中的节点?
How to update the nodes in the hash Map in specific key in java?
我想从链表中删除所有偶数节点。如何更新散列图特定键中的节点? 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 中,但我尝试做的是从头到尾进行探索,并保留我探索的前一个节点的引用。为什么?因为当我找到偶数节点时,我想将前一个节点连接到下一个节点,以便删除当前节点。我有偶数节点是头的边缘情况,因为没有分配前一个节点。
无法真正尝试此代码,但也许可以作为您尝试执行的操作的参考。
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){
但 temp
在循环后为 null 并且没有引用头部。
- 我在这里看不到使用
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
我想从链表中删除所有偶数节点。如何更新散列图特定键中的节点? 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 中,但我尝试做的是从头到尾进行探索,并保留我探索的前一个节点的引用。为什么?因为当我找到偶数节点时,我想将前一个节点连接到下一个节点,以便删除当前节点。我有偶数节点是头的边缘情况,因为没有分配前一个节点。
无法真正尝试此代码,但也许可以作为您尝试执行的操作的参考。
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){
但temp
在循环后为 null 并且没有引用头部。 - 我在这里看不到使用
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