/**
* 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)
dummyHead.next = head
let cur = dummyHead
while (head) {
if (head.val === val) {
cur.next = head.next
} else {
cur = cur.next
}
head = head.next
}
return dummyHead.next
};
function Node(val) {
this.val = val
this.next = null
}
var MyLinkedList = function () {
this.dummyHead = new Node(0)
this.size = 0
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function (index) {
if (index < 0 || index >= this.size) {
return -1
}
let cur = this.dummyHead
for (let i = 0; i <= index; i++) {
cur = cur.next
}
return cur.val
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function (val) {
this.addAtIndex(0, val)
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function (val) {
this.addAtIndex(this.size, val)
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
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 node = new Node(val)
node.next = pre.next
pre.next = node
this.size++
};
/**
* @param {number} index
* @return {void}
*/
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--
};
/**
* 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)
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function (head) {
let pre = null
while (head) {
const cur = head
head = head.next
cur.next = pre
pre = cur
}
return pre
};
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function (head) {
// 这道题画画图就容易出来了
let dummyHead = new ListNode(0)
dummyHead.next = head
let fast = dummyHead
while (fast.next && fast.next.next) {
let cur = fast.next
fast.next = cur.next
cur.next = cur.next.next
fast.next.next = cur
fast = cur
}
return dummyHead.next
};
19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
/**
* 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} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
let dummyHead = new ListNode(0)
dummyHead.next = head
let fast = dummyHead, slow = dummyHead
// 先让一个快指针走
for (let i = 0; i < n + 1; i++) {
fast = fast.next
}
while (fast) {
fast = fast.next
slow = slow.next
}
slow.next = slow.next.next
return dummyHead.next
};
面试题 02.07. 链表相交 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function (headA, headB) {
let lenA = 0, lenB = 0
let curA = headA, curB = headB
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
};
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
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
}
let cur = head
while (cur !== slow) {
cur = cur.next
slow = slow.next
}
return cur
};