公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

链表是具有不同长度的数据结构,任何节点都可以删除或添加到链表中。在本教程中,我们将实现一个完整的程序,用于在具有空间和时间复杂度的链表中插入节点。让我们首先了解问题陈述。

问题简介

在给定的问题中,我们给出一个链表,由于我们可以通过在链表中添加或删除节点来更改链表的大小,因此我们将在链表中添加或插入节点。

在链表中,我们可以在三个不同的位置添加新节点:最前面的节点、最后一个节点之后以及链表的中间。例如,给定的链表是 –

1 -> 2 -> 3 -> 4 -> 5 -> null,我们必须添加一个值为 9 的随机节点。因此,有很多情况需要添加节点,例如 –

    在起始处添加节点 – 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> null

    在中间添加节点 – 1 -> 2 -> 3 -> 7 -> 4 -> 5 -> null

    在末尾添加节点 – 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> null

    让我们看看实现以下任务的方法 –

    在链表开头添加节点

    示例

    要在链表的开头添加节点,我们必须创建新节点并将链表的头作为下一个节点传递给新节点,然后将头移动到新节点,添加新节点节点到链表的开头。

    // creating the linked list node
    class Node {
       constructor(data) {
          this.value = data;
          this.next = null;
       }
    }
    function push(tail, data){
       var new_node = new Node(data);
       tail.next = new_node;
       tail = tail.next;
       return tail
    }
    function add(data) {
       var new_node = new Node(data);
       new_node.next = head;
       return new_node;
    }
    
    var head = new Node(1);
    var tail = head;
    tail = push(tail, 2)
    tail = push(tail, 3)
    tail = push(tail, 4)
    tail = push(tail, 5)
    
    head = add(7);
    var data = 0;
    while(head != null) {
       data = data + head.value + " -> ";
       head = head.next;
    }
    console.log("Linked List after adding a node at starting: ")
    console.log(data + "null")
    

    登录后复制

    上述代码的时间复杂度为 O(1),因为我们只需移动一个指针,同样没有使用额外的空间,使得空间复杂度为 O(1)。

    在链表中间添加节点

    示例

    要在链表中间添加节点,我们必须创建新节点并传递该节点,然后才能将链表的新节点添加为新节点的下一个节点,这会添加新节点节点到中间的链表。

    // creating the linked list node
    class Node {
       constructor(data) {
          this.value = data;
          this.next = null;
       }
    }
    function push(tail, data) {
       var new_node = new Node(data);
       tail.next = new_node;
       tail = tail.next;
       return tail
    }
    function add(data,head) {
       var new_node = new Node(data);
       var temp = head;
       while(temp.value != 3) {
          temp  = temp.next;
       }
       new_node.next = temp.next;
       temp.next = new_node;
       return head;
    }
    var head = new Node(1);
    var tail = head;
    tail = push(tail, 2)
    tail = push(tail, 3)
    tail = push(tail, 4)
    tail = push(tail, 5)
    head = add(7,head);
    var data = 0;
    while(head != null) {
       data = data + head.value + " -> ";
       head = head.next;
    }
    console.log("Linked List after adding node in middle:")
    console.log(data + "null")
    

    登录后复制

    上述代码的时间复杂度是 O(N),因为我们必须移动到需要添加新节点的节点。上述过程的空间复杂度是 O(1),因为我们没有使用任何额外的空间。

    在链表末尾添加节点

    示例

    要在链表末尾添加节点,我们必须创建一个新节点,并将该节点添加到尾节点之后,并将尾节点移动到下一个节点。

    // creating the linked list node
    class Node {
       constructor(data) {
          this.value = data;
          this.next = null;
       }
    }
    function push(tail, data) {
       var new_node = new Node(data);
       tail.next = new_node;
       tail = tail.next;
       return tail
       }
    
    function add(data) {
       var new_node = new Node(data);
       tail.next = new_node;
       tail = tail.next
       return tail;
    }
    var head = new Node(1);
    var tail = head;
    tail = push(tail, 2)
    tail = push(tail, 3)
    tail = push(tail, 4)
    tail = push(tail, 5)
    
    tail = add(7);
    var data = 0;
    while(head != null){
       data = data + head.value + " -> ";
       head = head.next;
    }
    console.log("Linked List after adding a node at the end: ")
    console.log(data + "null")
    

    登录后复制

    上述代码的时间复杂度为 O(1),因为我们只需移动一个指针,同样没有使用额外的空间,使得空间复杂度为 O(1)。

    结论

    在上面的教程中,我们学习了如何通过三种可能的方式在现有链表中添加新节点。我们已经看到了带有解释的正确代码以及时间和空间复杂性。在链表中间添加一个节点需要 O(N) 时间,而对于其他两种情况,其时间复杂度为 O(1),并且对于所有三种可能性,空间复杂度都是 O(1)。

    以上就是在链表中插入节点的 JavaScript 程序的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:javascript 插入 程序 节点 链表
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定