【链表】【打卡50天】《剑指Offer》2刷:剑指 Offer 35. 复杂链表的复制,2种方法

1、题目描述

方法一:使用哈希表

 2、算法分析

本题是复杂链表的复制:

普通链表都是next指针指向后面的链表,现在的是两个指针next,random指针指向后面结点,random是随机指针方向

步骤如下:

使用Map集合进行映射,key是当前链表的结点,value是新链表的结点

①链表结点的复制,建立“原结点——>新结点”的映射

②构建新链表结点的next,random指针方向

③返回新链表结点的头结点

如图:

 

 

 

 

 

3、代码实现

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/

import java.util.*;
// 使用 Map ,key存储的是原链表结点,value存储的是新链表的结点 
class Solution {
    public Node copyRandomList(Node head) {
        if(head == null){
            return null;
        }
        Node cur = head;
        
        Map<Node,Node> map = new HashMap<>();
        // 1.复制各结点,建立“原结点-->新结点”的映射 
        while(cur != null){
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        // 2.构建新链表 next和random的方向
        // 结点有2个指针,一个是next,一个是random指针
        cur = head;
        while(cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        // 3. 返回新链表的头结点,key对应的值value,对应新的链表
        return map.get(head);
    }
}

方法二:使用复制拆分

 

具体看代码

把RandomListNode 修改成Node;label修改成val即可

/*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
// 1.复制链表复制每个结点,复制结点A得到A1,将A1插到结点A后面
// 2.重新遍历链表,复制原链表的随机指针random指向的结点到新结点,A1.next = A.random.next
// 3.拆分链表,将链表拆分为原链表和复制后的链表
import java.util.*;
public class Solution {
    public RandomListNode Clone(RandomListNode pHead) {
        if(pHead == null){
            return null;
        }
        RandomListNode currentNode = pHead;
        // 1、插入新结点,cloneNode是复制的结点,结点A得到A1,将结点A1插到结点A后面
        while(currentNode != null){
            // 创建一个
            RandomListNode cloneNode = new RandomListNode(currentNode.label);
            RandomListNode nextNode = currentNode.next;
            currentNode.next = cloneNode;
            cloneNode.next = nextNode;
            currentNode = nextNode;
        }
        currentNode = pHead;
        // 2、 重新遍历链表,复制原来结点的随机指针给新结点
        while(currentNode != null){
            currentNode.next.random = currentNode.random == null?null:currentNode.random.next;
            currentNode = currentNode.next.next;
        }
        
        // 拆分链表,将链表拆分为原链表和复制后的链表
        currentNode = pHead;
        RandomListNode pCloneHead = pHead.next;
        while(currentNode != null){
            RandomListNode cloneNode = currentNode.next;
            currentNode.next = cloneNode.next;
            cloneNode.next = cloneNode.next == null?null:cloneNode.next.next;
            currentNode = currentNode.next;
        }
        return pCloneHead;
        
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值