链表算法汇总
移除链表元素
leetcode.cn/problems/re…
var removeElements = function (head, val) {
const dummyHead = new ListNode(0)
dummyHead.next = head
let pre = dummyHead
while (head) {
if (head.val === val) {
pre.next = head.next
} else {
pre = head
}
head = head.next
}
return dummyHead.next
}
设计链表
leetcode.cn/problems/de…
class ListNode {
constructor(val) {
this.val = val
this.next = null
}
}
var MyLinkedList = function () {
this.dummyHead = new ListNode(0)
this.size = 0
}
MyLinkedList.prototype.get = function (index) {
if (index < 0 || index >= this.size) {
return -1
}
let node = this.dummyHead
for (let i = 0; i <= index; i++) {
node = node.next
}
return node.val
}
MyLinkedList.prototype.addAtHead = function (val) {
this.addAtIndex(0, val)
}
MyLinkedList.prototype.addAtTail = function (val) {
this.addAtIndex(this.size, val)
}
MyLinkedList.prototype.addAtIndex = function (index, val) {
if (index < 0 || index > this.size) {
return
}
let pre = this.dummyHead
for (let i = 0; i < index; i++) {
pre = pre.next
}
const addNode = new ListNode(val)
addNode.next = pre.next
pre.next = addNode
this.size++
}
MyLinkedList.prototype.deleteAtIndex = function (index) {
if (index < 0 || index >= this.size) {
return
}
let pre = this.dummyHead
for (let i = 0; i < index; i++) {
pre = pre.next
}
pre.next = pre.next.next
this.size--
}
反转链表
leetcode.cn/problems/re…
var reverseList = function (head) {
let pre = null
while (head) {
const cur = head
head = head.next
cur.next = pre
pre = cur
}
return pre
}
两两交换链表中的节点
leetcode.cn/problems/sw…
var swapPairs = function (head) {
const dummyHead = new ListNode(0)
dummyHead.next = head
let pre = dummyHead
while (pre.next && pre.next.next) {
const cur = pre.next
pre.next = cur.next
cur.next = cur.next.next
pre.next.next = cur
pre = cur
}
return dummyHead.next
}
删除链表的倒数第 N 个节点
leetcode.cn/problems/re…
var removeNthFromEnd = function (head, n) {
const dummyHead = new ListNode(0)
dummyHead.next = head
let node = dummyHead
for (let i = 0; i < n + 1; i++) {
node = node.next
}
let pre = dummyHead
while (node) {
pre = pre.next
node = node.next
}
pre.next = pre.next.next
return dummyHead.next
}
相交链表
leetcode.cn/problems/in…
var getIntersectionNode = function (headA, headB) {
let curA = headA,
curB = headB
let lenA = 0,
lenB = 0
while (curA) {
curA = curA.next
lenA++
}
while (curB) {
curB = curB.next
lenB++
}
let curL = lenA > lenB ? headA : headB
let curS = curL === headA ? headB : headA
const steps = Math.abs(lenA - lenB)
for (let i = 0; i < steps; i++) {
curL = curL.next
}
while (curL) {
if (curL === curS) {
return curL
}
curL = curL.next
curS = curS.next
}
return null
}
环形链表
leetcode.cn/problems/li…
var hasCycle = function (head) {
let fast = head,
slow = head
while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
if (fast === slow) break
}
if (!fast || !fast.next) return false
return true
}
环形链表 II
leetcode.cn/problems/li…
var detectCycle = function (head) {
let fast = head,
slow = head
while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
if (fast === slow) break
}
if (!fast || !fast.next) return null
slow = head
while (fast !== slow) {
fast = fast.next
slow = slow.next
}
return fast
}