文章目录
代码随想录
203. 移除链表元素
使用虚拟头结点。
var removeElements = function (head, val) {
// 头结点:下一个是head
const ans = new ListNode(0, head)
let now = ans;
// 从头结点开始遍历
while (now.next) {
if (now.next.val === val) {
// 跳过当前节点
now.next = now.next.next;
continue;
}
// 指针后移
now = now.next;
}
return ans.next;
};
707. 设计链表
- 细节很多的链表题
- 建议封装“获取某个节点”的方法
- 注意头尾细节:
- 对于
addAtHead
,如果链表为空,头节点也是尾节点 - 对于
addAtTail
,如果链表为空,尾节点也是头节点 - 对于
deleteAtIndex
,如果索引为0,要改变this._head
,如果索引为最后一个节点,要改变this._tail
- 注意
null
没有next
- 每次改变头尾节点时要改变
this._tail
和this._head
- 改变长度时要改变
this._size
- 链表题一般可以用虚拟头节点
// 创建节点
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
// 记录链表信息
var MyLinkedList = function () {
this._size = 0;
this._head = null;
this._tail = null;
};
MyLinkedList.prototype.getNode = function (index) {
// 虚拟头结点,下一个是head
let now = new LinkNode(0, this._head);
while (index >= 0) {
now = now.next;
index--;
}
return now;
}
/**
* @param {number} index
* @return {number}
*/
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 newHead = new LinkNode(val, this._head);
this._head = newHead;
this._size++;
// 特判:若原链表为空,头就是尾
if (!this._tail) {
this._tail = newHead;
}
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function (val) {
let newTail = new LinkNode(val, null);
this._size++;
// 若链表为空,头是尾
if (!this._tail) {
this._head = newTail;
this._tail = newTail;
} else {
this._tail.next = newTail;
this._tail = newTail;
}
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function (index, val) {
if (index <= 0) {
this.addAtHead(val);
} else if (index > this._size) {
return;
} else if (index === this._size) {
this.addAtTail(val)
} else {
let pre = this.getNode(index - 1);
let newNode = new LinkNode(val, pre.next);
pre.next = newNode;
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;
} else if (index === this._size - 1) {
// 删最后一个
let pre = this.getNode(index - 1);
pre.next = null;
this._tail = pre;
} else {
let pre = this.getNode(index - 1);
pre.next = pre.next.next;
}
this._size--;
};
206. 反转链表(双指针)
- 双指针
- 注意
[]
和[1]
的情况 - 反转后
head.next
为null
,否则会有循环节点 - 结束的条件:b指针为空,注意,在
b===null
的上一轮c已经为null
,则它没有next
var reverseList = function (head) {
if (head === null || head.next === null) return head;
let a = head, b = head.next, c = b.next;
a.next = null;
while (b) {
b.next = a;
a = b;
b = c;
if (c) c = c.next;
}
return a;
};
24. 两两交换链表中的节点(双指针)
思路:
- 双指针
- 如果是奇数的话,那么只有前偶数个才会互换,因此跳出循环的条件是b、c均不为null
- 若是奇数,则最后的b为null,无next,要特判一下
- 建议用虚拟头节点
var swapPairs = function (head) {
if (!head || !head.next) return head;
let newPre = new ListNode(0, head), a = newPre;
let b = a.next, c = b.next;
while (b && c) {
b.next = c.next;
a.next = c;
c.next = b;
a = b;
b = b.next;
if (b) c = b.next;
}
return newPre.next;
};
19. 删除链表的倒数第 N 个结点(双指针)
- 双指针:快慢指针
var removeNthFromEnd = function (head, n) {
let newPre = new ListNode(0, head);
let now1 = newPre, now2 = now1;
for (let i = 0; i < n; i++) {
now1 = now1.next;
}
while (now1.next) {
now1 = now1.next;
now2 = now2.next;
}
// now1为尾节点
now2.next = now2.next.next;
return newPre.next;
};
面试题 02.07. 链表相交
思路:
- 计算链表长度,长的先走几步,直到它们从起始位置到终止位置长度相同
var getIntersectionNode = function (headA, headB) {
let a = 0, b = 0;
let pa = headA, pb = headB;
while (pa) {
a++; pa = pa.next;
}
while (pb) {
b++; pb = pb.next;
}
pa = headA, pb = headB;
if (a < b) {
while (a < b) {
pb = pb.next; b--;
}
} else if (a > b) {
while (a > b) {
pa = pa.next; a--;
}
}
while (pa != pb && pa && pb) {
pa = pa.next;
pb = pb.next;
}
if (pa) return pa;
else return null;
};
142. 环形链表 II(双指针)
- 双指针:快慢指针ij
- 追及问题:快指针j一次两步,慢指针i一次一步,若有环则j会刚好在比i多一圈时追上i
- 若无环,快指针会先到null,若有环,永远不可能到null(因此不能计数)
- 如何计算环入口:在相遇时,在head处新加入指针k,一起向前,会在环入口处相遇,证明在下文
动图(来自代码随想录):
数学推导:
设头结点到环入口处为x,环入口到环尾为y,则环的长度为y+1。设从入口到相遇的点的距离为z,则有:
慢指针:x+z
快指针:x+z+y+1
ps:快指针一次两步,慢指针一次一步,则快指针在相遇时会比慢指针快一圈(追及问题),且快指针的长度是慢指针的两倍:
则2(x+z)=x+z+y+1,即x+z=y+1
从相遇处到入口的距离为:y+1-z(环的长度减去入口到相遇位置),即x,而x就是我们要求的头结点到环入口的长度。证毕
var detectCycle = function (head) {
let i = head, j = head;
while (i && j) {
i = i.next;
j = j.next;
if (j) j = j.next;
if (i === j) break;
}
// 有环
if (i === j && i && j) {
// 这里相遇
let k = head;
while (k !== i) {
k = k.next;
i = i.next;
}
return k;
} else {
// 无环
return null;
}
};