我正在尝试合并两个链表,但由于某种原因,当我的值被定义时,它显示无法读取null的值。
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
}
add(value) {
var node = new Node(value);
var current;
if (this.head == null) {
this.head = node
} else {
current = this.head
while (current.next) {
current = current.next;
}
current.next = node
}
this.length++
}
}
function mergeLinkedList(l1, l2) {
let newList = new LinkedList()
let currentNode1 = l1.head
let currentNode2 = l2.head
while (currentNode1 || currentNode2) {
if (currentNode1.value <= currentNode2.value) {
newList.add(currentNode1)
newList.add(currentNode2)
} else {
newList.add(currentNode2)
newList.add(currentNode1)
}
currentNode1 = currentNode1.next
currentNode2.head = currentNode2.next
}
return newList
}
let l1 = new LinkedList()
l1.add(1)
l1.add(2)
l1.add(4)
let l2 = new LinkedList()
l2.add(1)
l2.add(3)
l2.add(4)
mergeLinkedList(l1, l2)这应该返回一个新的链表,该链表的值是空的,但是在命中if语句时返回一个错误:if (currentNode1.value <= currentNode2.value),声明它不能读取null的属性‘1->1->2->3->4->4’,但是这个值在这里定义得很清楚。
发布于 2019-03-29 05:40:19
你有
while (currentNode1 || currentNode2)
因此,只需要在while循环中有一个节点不为null即可。但在那之后你就有了
if (currentNode1.value <= currentNode2.value)
它正在访问这两个节点的值。在这种情况下,您可能在while循环中使用currentNode2 == null,从而导致错误。
您应该将while循环条件编写为
while (currentNode1 && currentNode2){
.
.
.
}然后有另一个条件来检查您的链表,并附加一个还不为空的链表的其余部分。
发布于 2019-03-29 05:50:56
您可以独立地检查节点并添加值,而不是节点本身。
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
}
add(value) {
var node = new Node(value);
var current;
if (this.head == null) {
this.head = node
} else {
current = this.head
while (current.next) {
current = current.next;
}
current.next = node
}
this.length++
}
}
function mergeLinkedList(l1, l2) {
let newList = new LinkedList()
let currentNode1 = l1.head
let currentNode2 = l2.head
while (currentNode1 && currentNode2) {
if (currentNode1.value <= currentNode2.value) {
newList.add(currentNode1.value)
currentNode1 = currentNode1.next
} else {
newList.add(currentNode2.value)
currentNode2 = currentNode2.next
}
}
while (currentNode1) {
newList.add(currentNode1.value)
currentNode1 = currentNode1.next
}
while (currentNode2) {
newList.add(currentNode2.value)
currentNode2 = currentNode2.next
}
return newList
}
let l1 = new LinkedList()
l1.add(1)
l1.add(2)
l1.add(4)
console.log(l1);
let l2 = new LinkedList()
l2.add(1)
l2.add(3)
l2.add(4)
console.log(l2);
console.log(mergeLinkedList(l1, l2)).as-console-wrapper { max-height: 100% !important; top: 0; }
发布于 2022-03-04 00:06:11

通过查看上图:
var mergeTwoListss = function(l1, l2) {
// initialize a dummy node, make a copy and iterate through with the copy.
// after iteration complete, you still have the reference to initial point
const dummy=new ListNode()
let tail= dummy
// if you are comparing values in lists, make sure you have elements to compare
while (l1 && l2){
if (l1.val<l2.val){
tail.next=l1
l1=l1.next
}else{
tail.next=l2
l2=l2.next
}
tail=tail.next
}
// so far we have iterated for the same size of both nodes. what if one of them is longer.
if(l1){
tail.next=l1
}else if (l2){
tail.next=l2
}
return dummy.next
};https://stackoverflow.com/questions/55407113
复制相似问题