【问题标题】:How to update the nodes in the hash Map in specific key in java?java - 如何在java中的特定键中更新哈希映射中的节点?
【发布时间】: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


【解决方案1】:
  • 在最后一个循环中,您编写了while(temp!=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

【讨论】:

    【解决方案2】:

    在对链表进行操作时,通常不需要 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      相关资源
      最近更新 更多