138复制带随机指针的链表
思路:链表的深拷贝可以利用到HashMap,进行两次遍历,第一次进行赋初始值,第二遍对random和next值进行初始化
public Node copyRandomList(Node head) {
    if(head==null)return null;
    HashMap<Node ,Node>map=new HashMap<>();
    Node temp=head;
    while(temp!=null){
        map.put(temp,new Node(temp.val));
        temp=temp.next;
    }
    temp=head;
    while (temp!=null){
        map.get(temp).next=map.get(temp.next);
        map.get(temp).random=map.get(temp.random);
        temp=temp.next;
    }
    return map.get(head);
}
 
 

相关文章:

  • 2021-04-08
  • 2021-11-01
  • 2021-11-07
  • 2022-03-05
  • 2021-11-07
  • 2021-07-14
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2021-07-09
  • 2021-04-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案