
链表是具有不同长度的数据结构,任何节点都可以删除或添加到链表中。在本教程中,我们将实现一个完整的程序,用于在具有空间和时间复杂度的链表中插入节点。让我们首先了解问题陈述。
在给定的问题中,我们给出一个链表,由于我们可以通过在链表中添加或删除节点来更改链表的大小,因此我们将在链表中添加或插入节点。
在链表中,我们可以在三个不同的位置添加新节点:最前面的节点、最后一个节点之后以及链表的中间。例如,给定的链表是 -
1 -> 2 -> 3 -> 4 -> 5 -> null,我们必须添加一个值为 9 的随机节点。因此,有很多情况需要添加节点,例如 -
在起始处添加节点 - 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
立即学习“Java免费学习笔记(深入)”;
在中间添加节点 - 1 -> 2 -> 3 -> 7 -> 4 -> 5 -> null
在末尾添加节点 - 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> null
让我们看看实现以下任务的方法 -
ReportPlust意在打造一套精美的数据报表模板,里面高度封装日历组件、表格组件、排行榜组件、条形进度条组件、文本块组件以及ucharts的多个图表组件,用户只需要按照虚拟数据的格式,传特定数据即可方便、快捷地打造出属于自己的报表页面。该小程序主要使用了ucharts和wyb-table两插件实现的数据报表功能。 特点使用的是uni-app中最受欢迎的图表uCharts插件完成图表展示,该插件
0
要在链表的开头添加节点,我们必须创建新节点并将链表的头作为下一个节点传递给新节点,然后将头移动到新节点,添加新节点节点到链表的开头。
// 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 程序的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号