链表理论基础
理论
概念
链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。
类型
单链表,双链表,循环链表
存储方式
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
代码
class ListNode {
val;
next = null;
constructor(value) {
this.val = value;
this.next = null;
}
}
203.移除链表元素
题目
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
提示:
列表中的节点数目在范围 [0, 10^4] 内
1 <= Node.val <= 50
0 <= val <= 50
方法
思路很简单 有几个需要注意的点
-
设置虚拟头结点,可以让符合条件的头结点和一般节点处理方式相同
-
在找到符合条件的节点删除后,我们不能cur = cur.next;不然会漏掉一个节点没有查找(此种情况会出现在:连续两个符合条件的节点)
【需要在草稿纸上模拟遍历一遍】
代码
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var removeElements = function(head, val) {
let dummyHead = new ListNode(0,head);
let cur = dummyHead;
while (cur.next){
if (cur.next.val == val){
cur.next = cur.next.next;
continue;
}
cur = cur.next;
}
return dummyHead.next;
};
707.设计链表
题目
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。
如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。
实现 MyLinkedList 类:
- MyLinkedList() 初始化 MyLinkedList 对象。
- int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1 。
- void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
- void addAtTail(int val) 将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
- void addAtIndex(int index, int val) 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index 比长度更大,该节点将 不会插入 到链表中。
- void deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index 的节点。
示例:
输入
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
输出
[null, null, null, null, 2, null, 3]
解释
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // 链表变为 1->2->3
myLinkedList.get(1); // 返回 2
myLinkedList.deleteAtIndex(1); // 现在,链表变为 1->3
myLinkedList.get(1); // 返回 3
提示:
- 0 <= index, val <= 1000
- 请不要使用内置的 LinkedList 库。
- 调用 get、addAtHead、addAtTail、addAtIndex 和 deleteAtIndex 的次数不超过 2000
代码
class LinkNode {
constructor (val,next){
this.val = val;
this.next = next;
}
}
var MyLinkedList = function() {
this._size = 0;
this._head = null;
this._tail = null;
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.getNode = function(index) {
if (index < 0 || index >= this._size) {
return null;
}
let cur = new LinkNode(0,this._head);
while (index-- >= 0){
cur = cur.next;
}
return cur;
}
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this._size) {
return -1;
}
return this.getNode(index).val;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
let node = new LinkNode(val,this._head);
this._head = node;
this._size++;
if (!this._tail){
this._tail = node;
}
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
let node = new LinkNode(val,null);
this._size++;
if (!this._tail){
this._tail = node;
this._head = node;
}
this._tail.next = node;
this._tail = node;
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index <= 0){
this.addAtHead(val);
return;
}
if (index == this._size){
this.addAtTail(val);
return;
}
if (index > this._size) return null;
// let node = new LinkNode(val,null);
// this._size++;
// let cur = getNode(index - 1);
// node.next = cur.next;
// cur.next = node;
let node = this.getNode(index - 1);
node.next = new LinkNode(val,node.next);
this._size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this._size) return;
// 删除操作头结点要注意
if (index == 0) {
this._head = this._head.next;
// 如果要删除的节点同时还是个尾节点
if (index == this._size - 1) {
this._tail = this._head;
}
this._size--;
return;
}
let node = this.getNode(index - 1);
node.next = node.next.next;
// 如果是尾节点 那么需要更换一下尾节点的指针
if (index == this._size - 1){
this._tail = node;
}
this._size--;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* var obj = new MyLinkedList()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/
tips:
-
定义原型函数用到属性和方法时,都需要加this例如this._size,this_getNode()
-
getNode()方法需要掌握
while (index-- >= 0) 很巧妙
-
要注意头尾节点的添加删除
(做完题目后要验证一下头尾是否符合我们的一般方法)