其他分享
首页 > 其他分享> > 剑指offer-2.27-35

剑指offer-2.27-35

作者:互联网

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/*
// 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;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        if (head == null)
        return null;
    // 插入新节点
    Node cur = head;
    while (cur != null) {
        Node clone = new Node(cur.val);
        clone.next = cur.next;
        cur.next = clone;
        cur = clone.next;
    }
    // 建立 random 链接
    cur = head;
    while (cur != null) {
        Node clone = cur.next;
        if (cur.random != null)
            clone.random = cur.random.next;
        cur = clone.next;
    }
    // 拆分
    cur = head;
    Node cloneHead = head.next;
    while (cur.next != null) {
        Node next = cur.next;
        cur.next = next.next;
        cur = next;
    }
    return cloneHead;
    }
}

标签:Node,cur,offer,clone,random,35,next,null,2.27
来源: https://blog.csdn.net/Desperate_gh/article/details/114198694