You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode 138题复制带随机指针链表JS代码提交报错求助

138. 复制带随机指针的链表

给定一个长度为n的链表,每个节点包含一个额外的随机指针,该指针可以指向链表中的任意节点或null。
构造该链表的深拷贝。深拷贝应由n个全新节点组成,每个新节点的值都与原链表对应节点的值相同。新节点的next和random指针都应指向拷贝链表中的新节点,使得原链表和拷贝链表的指针状态一致。拷贝链表中的指针不得指向原链表的节点。

我的实现代码如下:

function Node(val, next, random) {
    this.val = val;
    this.next = next;
    this.random = random;
};
var copyRandomList = function (head) {
    const old2NewMap = new Map()

    let pointer = head;
    while (pointer) {
        const tempNewNode = new Node(pointer.val);
        old2NewMap.set(pointer, tempNewNode)
        pointer = pointer.next;
    }

    pointer = head;
    while (pointer) {
        const tempNewNode = old2NewMap.get(pointer);
        tempNewNode.next = old2NewMap.get(pointer.next)
        tempNewNode.random = old2NewMap.get(pointer.random)
        pointer = pointer.next;
    }

    return old2NewMap.get(head)
};

本地测试代码运行正常,测试代码如下:

const th1Node = new Node(7, null, null)
const th2Node = new Node(13, null, null)
const th3Node = new Node(11, null, null)
const th4Node = new Node(10, null, null)
const th5Node = new Node(1, null, null)

th1Node.next = th2Node;
th1Node.random = null;

th2Node.next = th3Node;
th2Node.random = th1Node;

th3Node.next = th4Node;
th3Node.random = th5Node;

th4Node.next = th5Node;
th4Node.random = th3Node;

th5Node.next = null;
th5Node.random = th1Node;

console.log(copyRandomList(th1Node))

但提交到LeetCode时出现错误,错误信息:

Line 83 in solution.js
            node = node.next;
                        ^
TypeError: Cannot read properties of undefined (reading 'next')
    Line 83: Char 25 in solution.js (Object.serializer.isCyclic)
    Line 109: Char 24 in solution.js (Object.serializer.serializeLinkedList)
    Line 165: Char 30 in solution.js (Object.<anonymous>)
    Line 16: Char 8 in runner.js (Object.runner)
    Line 28: Char 26 in solution.js (Object.<anonymous>)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

问题原因与解决方法

这个错误是LeetCode的序列化代码遍历拷贝链表时触发的,根源在于拷贝后的链表中部分节点的next或random字段是undefined而非null:

  • 当原节点的next或random为null时,old2NewMap.get(null)会返回undefined,而代码直接将这个值赋值给新节点的对应字段,导致链表结构不符合预期。LeetCode的序列化逻辑会尝试遍历undefined的next,从而抛出错误。

修改方案:

  1. 给next和random的赋值加上默认值null,确保空指针统一为null而非undefined;
  2. 增加空链表的边界处理,避免输入head为null时返回undefined。

修改后的完整代码:

function Node(val, next, random) {
    this.val = val;
    this.next = next;
    this.random = random;
};
var copyRandomList = function (head) {
    if (!head) return null;
    const old2NewMap = new Map()

    let pointer = head;
    while (pointer) {
        const tempNewNode = new Node(pointer.val);
        old2NewMap.set(pointer, tempNewNode)
        pointer = pointer.next;
    }

    pointer = head;
    while (pointer) {
        const tempNewNode = old2NewMap.get(pointer);
        tempNewNode.next = old2NewMap.get(pointer.next) ?? null;
        tempNewNode.random = old2NewMap.get(pointer.random) ?? null;
        pointer = pointer.next;
    }

    return old2NewMap.get(head)
};

内容的提问来源于stack exchange,提问作者hh54188

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:05:36