力扣解题思路:复杂链表、图的复制

NowCoder:复杂链表的复制


思路:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的 head。
这一题猛地一看好像很简单,只要复制相应的节点然后穿起来即可,但是这里有个问题,节点复制当然简单,但是要按照原始链表的random的指向,如何复制这个指向呢? 我们可以先把所有next指向先复制了,就可以把链表穿起来,但是在新链表中寻找random是个很复杂的事情,那么我们是否有更好的方法呢,比如我们知道random在原链表中的位置,那我们是否可以不用盲目的寻找random?->

下图为具体过程,该图参考自这里哦
在这里插入图片描述
按照上述步骤我们可以写出完整代码:

public RandomListNode Clone(RandomListNode pHead) {
    if (pHead == null)
        return null;
    // 插入新节点
    RandomListNode cur = pHead;
    while (cur != null) {
        RandomListNode clone = new RandomListNode(cur.label);
        clone.next = cur.next;
        cur.next = clone;
        cur = clone.next;
    }
    // 建立 random 链接
    cur = pHead;
    while (cur != null) {
        RandomListNode clone = cur.next;
        if (cur.random != null)
            clone.random = cur.random.next;
        cur = clone.next;
    }
    // 拆分
    cur = pHead;
    RandomListNode pCloneHead = pHead.next;
    while (cur != null) {
        RandomListNode next = cur.next;//cur.next一定不为空,因为此时节点都是成对的,不可能有cur而没有cur.next
        cur.next = next.next;//next.next可能已经为空了(后面没有节点了)
        //next.next = cur.next.next;
        next.next = cur.next == null ? null : cur.next.next;
        cur = cur.next;
    }
    return pCloneHead;
}

这里还需要注意的一点就是,在拆分链表的时候,两个链表都是需要被拆分的,不可以只拆分我们所需要的新链表(那样原始链表会被破坏),其次,在拆分的时候循环中的判断条件应该为cur != null,cur != null可以推出cur.next != null (写cur.next != null的话会在cur == null时报空指针异常),因为我们复制后每个节点都是有两个的,但是我们无法保证cur.next.next是否为空,所以还需要有 next.next = cur.next == null ? null : cur.next.next;的判断

另外,cur.random != null一定不能省!!!!不然cur.random.next会有空指针异常,还有clone.random = cur.random.next;不可以写成clone.random = cur.random;!!!!!!

133. 克隆图

思路:给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。题目参考这里

图的结构是这样的:

class Node {
    public int val;
    public List<Node> neighbors;
}

我们只需要将每个节点先一一拷贝,然后再按照原图的连接把拷贝出的节点的neighbors填上就可以了,可以选择BFS或者BFS来遍历整个图。先使用BFS,我们为了能获取每个节点的拷贝节点,我们使用Map来保存每个节点和其拷贝节点的映射关系,具体细节可以看注释:

public Node cloneGraph(Node node){
    if(node==null) return null;
    Map<Node,Node> lookup = new HashMap<>();
    Node clone = new Node(node.val,new ArrayList<>());
    lookup.put(node,clone);
    Queue<Node> queue = new LinkedList<>();
    queue.offer(node);
    while(!queue.isEmpty()){
        Node tmp = queue.poll();
        for(Node n:tmp.neighbors){
            if(!lookup.containsKey(n)){//判断其相邻节点是否已经被拷贝过一份了
                lookup.put(n,new Node(n.val,new ArrayList<>()));//若没有则直接拷贝
                queue.offer(n);//没有遍历过的才需要加入队列
            }
            lookup.get(tmp).neighbors.add(lookup.get(n));//为tmp的拷贝节点的neighbors赋值
        }
    }
    return clone;
}

按照同样的方法可以写出DFS版本,原理是一样的:

public Node cloneGraph(Node node) {
    Map<Node,Node> lookup = new HashMap<>();
    return dfs(node,lookup);
 }
 private Node dfs(Node node,Map<Node,Node> lookup){
     if(node == null) return null;
     if(lookup.containsKey(node)) return lookup.get(node);
     Node clone = new Node(node.val,new ArrayList<>());
     lookup.put(node,clone);
     for(Node n:node.neighbors) clone.neighbors.add(dfs(n,lookup));
     return clone;
 }

这两个题目的共同点是,先复制节点,然后再为复制的节点的属性赋值,并且必须要想出一个方法能通过原始节点来获得其复制节点(这一点非常重要!!)。第一个题目是通过原节点的next来获得复制节点的,第二个题目是通过Map中保存的原始节点到复制节点的映射来获得复制节点的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值