【问题标题】:how to insert an element in linked list by JavaScript如何通过 JavaScript 在链表中插入元素
【发布时间】:2020-10-03 15:52:42
【问题描述】:

我从 LeetCode 得到了这个问题

问题 21,https://leetcode.com/problems/merge-two-sorted-lists/
但不仅仅是为了解决这个问题

这是我对我的问题的描述我有一个原始链表[1,2,4],它的数据结构是这样的:

  function ListNode(val, next) {
      this.val = (val===undefined ? 0 : val)
      this.next = (next===undefined ? null : next)
  }

我想在 2 之后插入 3,并将其变为 [1,2,3,4]。
几乎从我读过的所有教程中,他们都告诉我这样做:

var insert = function(l1) {
    let i=0;
    let p = l1;
    while(i<1 && p){
        p = p.next;
        i++;
    }
    let tem = new ListNode(3,p.next);
    p.next = tem;
    return p;
};

但是 p 是 [2,3,4] 因为 p 在完成 while 循环时已经分配为 [2,4] ,显然这是不对的。
那么我该如何解决这个问题呢?
以及为什么教程说像
find the node(p) you want to insert after,and create a new node(q), and q.next = p.next ;p.next = q

【问题讨论】:

  • 提议的“算法”有哪些你不明白的地方?
  • 为什么在第一次迭代后停止while 循环?您甚至没有测试您当前在列表中的哪个节点。
  • @Andreas 很抱歉没有说清楚,我这样做是为了测试目的,为了直接获取节点 2,我对其进行了硬编码。
  • 尝试并实现这一点,看看你能走多远。如果您仍然遇到问题,请在此处将代码作为 sn-p 发布。这将使人们更容易为您提供帮助。

标签: javascript algorithm ecmascript-6 linked-list


【解决方案1】:

你不应该返回p,而应该返回lst

我当然建议不要在你的函数中硬编码值和循环条件,而是将它们作为参数传递给你的函数。

此外,当您想调用 insert 在列表的最开始插入一个值时,您需要一段单独的代码:

function ListNode(val=0, next=null) { // use defaults
    this.val = val;
    this.next = next;
}

var insert = function(l1, index, value) {
    if (index == 0) return new ListNode(value, l1);
    let i = 1; // start at 1
    let p = l1;
    while(i<index && p){
        p = p.next;
        i++;
    }
    let tem = new ListNode(value, p.next);
    p.next = tem;
    return lst; // return the list
};

function toArray(lst) { // utility to help display the list
    return lst ? [lst.val].concat(toArray(lst.next)) : [];
}

let lst = new ListNode(1, new ListNode(2, new ListNode(4)));

lst = insert(lst, 2, 3); // insert value 3 at index 2 

console.log(toArray(lst));

【讨论】:

  • 这正是我的误会,非常感谢,明白了
【解决方案2】:

请试试这个

class LinkedList{
    constructor(val){
        this.head = new ListNode(val);
        this.tail = this.head;
    }
    
    add(val){
        this.tail.next = new ListNode(val);
        this.tail = this.tail.next;
    }
    
    insert(val, after){
        let node = this;
      
        while(node){
                        //find the node you want to insert after
            if ( node.val === after){
                                //and create a new node,  and q.next = p.next ;p.next = q
                node.next = new ListNode(val, node.next);
                                //break
                node = null;
            } else {
                            //node is not fouund still
              node = node.next;
            }
            
        }
    }
}


class ListNode{
   constructor(val, next) {
      this.val = (val===undefined ? 0 : val);
      this.next = (next===undefined ? null : next);
   }
  }
  
  
  var list = new LinkedList(1);//1
  list.add(2);//1->2
  list.add(4);//1->2->4
  list.insert(3,2);//1->2->3->4
  

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多