英文:
I don't know why my JS code for LeetCode Copy List with Random Pointer question is wrong
问题
The issue in your code seems to be in the first while loop where you are iterating through the original linked list but not updating the pointer variable, causing an infinite loop and eventually leading to a "Cannot read properties of undefined" error. To fix this, you need to update the pointer inside the loop. Here's the corrected part of your code:
let pointer = head;
while (pointer) {
const tempNewNode = new Node(pointer.val);
old2NewMap.set(pointer, tempNewNode);
pointer = pointer.next; // Add this line to update the pointer
}
By adding pointer = pointer.next;
inside the first while loop, you ensure that the pointer
variable moves to the next node in the original linked list during each iteration. This should resolve the error you're encountering.
英文:
This is the leetcode question 138. Copy List with Random Pointer
> A linked list of length n is given such that each node contains an
> additional random pointer, which could point to any node in the list,
> or null.
>
> Construct a deep copy of the list. The deep copy should consist of
> exactly n brand new nodes, where each new node has its value set to
> the value of its corresponding original node. Both the next and random
> pointer of the new nodes should point to new nodes in the copied list
> such that the pointers in the original list and copied list represent
> the same list state. None of the pointers in the new list should point
> to nodes in the original list.
Here is my code:
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)
};
And it can passed on my local PC, at least no error. Here is the test code:
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))
However, when I submit it tell me wrong:
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)
I don't know why
答案1
得分: 0
`node.next` 应该在链表的最后一个节点上为 `null`。但当你执行以下操作时:
tempNewNode.next = old2NewMap.get(pointer.next)
`pointer.next` 是 `null`,而在 `old2NewMap` 中没有这个值的条目。`get()` 返回 `undefined`,而不是 `null`。你需要将其转换为 `null`。
tempNewNode.next = old2NewMap.get(pointer.next) || null;
英文:
node.next
is supposed to be null
for the last node in the list. But when you do:
tempNewNode.next = old2NewMap.get(pointer.next)
pointer.next
is null
, and there's no entry in old2NewMap
for this value. get()
returns undefined
, not null
. You need to convert that to null
.
tempNewNode.next = old2NewMap.get(pointer.next) || nulll
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论