首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法合并链表?

无法合并链表?
EN

Stack Overflow用户
提问于 2019-03-29 05:28:15
回答 3查看 136关注 0票数 2

我正在尝试合并两个链表,但由于某种原因,当我的值被定义时,它显示无法读取null的值。

代码语言:javascript
复制
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’,但是这个值在这里定义得很清楚。

EN

回答 3

Stack Overflow用户

发布于 2019-03-29 05:40:19

你有

while (currentNode1 || currentNode2)

因此,只需要在while循环中有一个节点不为null即可。但在那之后你就有了

if (currentNode1.value <= currentNode2.value)

它正在访问这两个节点的值。在这种情况下,您可能在while循环中使用currentNode2 == null,从而导致错误。

您应该将while循环条件编写为

代码语言:javascript
复制
 while (currentNode1 && currentNode2){
  .
  .
  .
 }

然后有另一个条件来检查您的链表,并附加一个还不为空的链表的其余部分。

票数 0
EN

Stack Overflow用户

发布于 2019-03-29 05:50:56

您可以独立地检查节点并添加值,而不是节点本身。

代码语言:javascript
复制
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))
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 0
EN

Stack Overflow用户

发布于 2022-03-04 00:06:11

通过查看上图:

代码语言:javascript
复制
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
    
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55407113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档