剑指 Offer 35. 复杂链表的复制

class Solution {
    HashMap<Node,Node> map = new HashMap<>();
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node p = head;
        while(p!=null){
            Node q = new Node(p.val);
            map.put(p,q);
            p = p.next;
        }
        p = head;
        while (p!=null){
            Node q = map.get(p);
            if(p.next!=null){
                q.next = map.get(p.next);
            }
            if(p.random!=null){
                q.random = map.get(p.random);
            }
            p = p.next;
        }
        return map.get(head);
    }
}

 

posted @ 2020-08-19 15:58  欣姐姐  阅读(87)  评论(0编辑  收藏  举报