链表的实现js
链表的基本概念
链表是一种线性数据结构,由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,动态增删节点更高效。
单链表的实现
class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// 在链表尾部添加节点
append(val) {
const newNode = new ListNode(val);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
this.size++;
}
// 在特定位置插入节点
insertAt(val, index) {
if (index < 0 || index > this.size) return false;
const newNode = new ListNode(val);
if (index === 0) {
newNode.next = this.head;
this.head = newNode;
} else {
let current = this.head;
for (let i = 0; i < index - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
this.size++;
return true;
}
// 删除特定位置的节点
removeFrom(index) {
if (index < 0 || index >= this.size) return null;
let removedNode;
if (index === 0) {
removedNode = this.head;
this.head = this.head.next;
} else {
let current = this.head;
for (let i = 0; i < index - 1; i++) {
current = current.next;
}
removedNode = current.next;
current.next = current.next.next;
}
this.size--;
return removedNode.val;
}
// 获取链表长度
getSize() {
return this.size;
}
// 打印链表
print() {
let current = this.head;
let str = '';
while (current) {
str += current.val + ' -> ';
current = current.next;
}
str += 'null';
console.log(str);
}
}
双向链表的实现
双向链表每个节点包含指向前驱和后继的指针,支持双向遍历。
class DoublyListNode {
constructor(val) {
this.val = val;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
append(val) {
const newNode = new DoublyListNode(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.size++;
}
insertAt(val, index) {
if (index < 0 || index > this.size) return false;
const newNode = new DoublyListNode(val);
if (index === 0) {
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
} else if (index === this.size) {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
} else {
let current = this.head;
for (let i = 0; i < index - 1; i++) {
current = current.next;
}
newNode.next = current.next;
newNode.prev = current;
current.next.prev = newNode;
current.next = newNode;
}
this.size++;
return true;
}
}
环形链表的实现
环形链表的尾节点指向头节点,形成闭环。
class CircularLinkedList {
constructor() {
this.head = null;
this.size = 0;
}
append(val) {
const newNode = new ListNode(val);
if (!this.head) {
this.head = newNode;
newNode.next = this.head;
} else {
let current = this.head;
while (current.next !== this.head) {
current = current.next;
}
current.next = newNode;
newNode.next = this.head;
}
this.size++;
}
}
链表操作的时间复杂度
- 访问元素:O(n)
- 插入/删除头部:O(1)
- 插入/删除尾部:O(1)(双向链表)或 O(n)(单链表)
- 插入/删除中间节点:O(n)
链表的应用场景
适合频繁插入删除操作的场景,如实现队列、栈、哈希表冲突解决等。相比数组,链表不需要预先分配内存空间,但访问效率较低。







