虚拟头节点
文档链接: 代码随想录
LeetCode 203.Remove Linked List Elements
/**
* 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) {
const vNode = new ListNode(0, head)
let slow = vNode
let fast = vNode.next
while (fast != null) {
if (fast.val == val) {
slow.next = fast.next
fast = fast.next
} else {
slow = fast
fast = fast.next
}
}
return vNode.next
};
设计链表
非常有意义,对链表的结构和操作更加熟悉
文档链接:代码随想录
LeetCode 707.Design Linked List
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
var MyLinkedList = function () {
this._size = 0;
this._tail = null;
this._head = null;
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function (index) {
if (index < 0 || index >= this._size) {
return -1
}
let vhead = new LinkNode(0, this._head)
while (index-- >= 0) {
vhead = vhead.next
}
return vhead.val
};
MyLinkedList.prototype.getNode = function (index) {
if (index < 0 || index >= this._size) {
return -1
}
let vhead = new LinkNode(0, this._head)
while (index-- >= 0) {
vhead = vhead.next
}
return vhead
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function (val) {
const nHead = new LinkNode(val, this._head)
this._size++
this._head = nHead
if (this._tail == null) {
this._tail = nHead
}
return nHead
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function (val) {
const nTail = new LinkNode(val, null)
if (this._tail != null) {
this._tail.next = nTail
this._tail = nTail
} else {
this._tail = nTail
this._head = nTail
}
this._size++
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function (index, val) {
if (index == this._size) {
this.addAtTail(val)
} else if (index > this._size || index < 0) {
return
} else if (index == 0) {
this.addAtHead(val)
} else {
let preNode = this.getNode(index - 1)
let nextNode = this.getNode(index)
const addedNode = new LinkNode(val, nextNode)
preNode.next = addedNode
this._size++
}
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function (index) {
console.log('size', this._size)
let vhead = new LinkNode(0, this._head)
while (vhead != null) {
console.log('index-->', vhead.val)
vhead = vhead.next
}
if (index < 0 || index >= this._size) {
return
}
if (index == 0) {
if (this._size == 1) {
this._head = null
this._tail = null
} else {
this._head = this.getNode(1)
}
} else {
const node = this.getNode(index - 1)
node.next = node.next.next
if (node.next == null) {
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)
*/
反转链表
文档链接: 代码随想录
LeetCode 206.Reverse Linked List
/**
* 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 fast = head
let slow = null
while (fast != null) {
let temp = fast.next
fast.next = slow
slow = fast
fast = temp
}
return slow
};