代码随想录算法训练营第三天 | 链表理论基础, LeetCode 203.移除链表元素, 707.设计链表, 206.反转链表
链表理论基础
链表分为单链表、双链表、循环链表等。
链表特点:
- 链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上。
- 链表没有索引
class ListNode {
val;
next = null;
// 构造器
constructor(value, node) {
this.val = value;
this.next = node;
}
}
LeetCode 203.移除链表元素
let removeElements = function (head, val) {
// 创建虚拟头节点
const dummy = new ListNode(0, head)
// 创建指针
let cur = dummy
// 如果下一个节点存在,就继续
while (cur.next){
if (cur.next.val === val){
// 删除下一个节点
cur.next = cur.next.next
} else {
// 如果不是目标值,指针向下一个节点移动
cur = cur.next
}
}
// 虚拟头节点的下一个节点即为新链表的头节点
return dummy.next
}
错误
错误在于写了用for循环写了一个错的: 以下两个循环并不相互等价
for(; cur.next !== null; cur = cur.next){
if(cur.next.val === val){
cur.next = cur.next.next
continue
}
}
for (; cur.next !== null; ) {
if (cur.next.val === val) {
cur.next = cur.next.next;
continue;
}
cur = cur.next;
}
continue是结束单次循环并跳入下一个循环。
以上第一个循环,不管continue了没,cur都要往下一个移动;
而第二个循环,只有在不满足if的条件中,cur才会向下一个移动
LeetCode 707.设计链表
// 定义节点类,表示链表中的一个节点,具有 val、next、prev 三个属性
class ListNode {
constructor(val, next = null, prev = null) {
this.val = val;
this.next = next;
this.prev = prev;
}
}
// 定义 MyLinkedList 类
class MyLinkedList {
constructor() {
// 初始化链表为空,头结点和尾结点都为 null,链表长度为 0
this.head = null;
this.tail = null;
this.size = 0;
}
// 获取链表中指定位置节点的值,如果位置无效则返回 -1
get(index) {
if (index < 0 || index >= this.size) return -1;
let cur = this.head;
for (let i = 0; i < index; i++) {
cur = cur.next;
}
return cur.val;
}
// 在链表头部插入一个值为 val 的新节点
addAtHead(val) {
const node = new ListNode(val, this.head);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.head.prev = node;
this.head = node;
}
this.size++;
}
// 在链表尾部插入一个值为 val 的新节点
addAtTail(val) {
const node = new ListNode(val, null, this.tail);
if (!this.tail) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.size++;
}
// 在链表中指定位置插入一个值为 val 的新节点
addAtIndex(index, val) {
if (index > this.size) return;
if (index <= 0) return this.addAtHead(val);
if (index === this.size) return this.addAtTail(val);
let cur = this.head;
for (let i = 0; i < index - 1; i++) {
cur = cur.next;
}
const node = new ListNode(val, cur.next, cur);
cur.next.prev = node;
cur.next = node;
this.size++;
}
// 删除链表中指定位置的节点
deleteAtIndex(index) {
if (index < 0 || index >= this.size) return;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else if (index === 0) {
this.head = this.head.next;
this.head.prev = null;
} else if (index === this.size - 1) {
this.tail = this.tail.prev;
this.tail.next = null;
} else {
let cur = this.head;
for (let i = 0; i < index; i++) {
cur = cur.next;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
this.size--;
}
}
LeetCode 206.反转链表
代码实现
let reverseList = function (head) {
let cur = null // 初始化 cur 为 null,表示反转后的链表的头结点为 null
let pre = head // pre 初始化为原链表的头结点
while (pre){
let temp = pre.next
pre.next = cur // 反转 pre 的指针指向 cur
cur = pre // cur 向前移动一步
pre = temp // pre 向前移动一步
}
return cur
}
总结
- 画图
- 想象动态的过程
- 考虑对于头/尾节点,空链表是否需要特殊处理
- 利用好链表自带指向下一个节点的指针这个特性