题目
解题
解题一:单链表
建议使用哨兵节点,如此一来插入和删除可以减少讨论(链表永远不为空),没有使用哨兵节点的方法的代码也贴在下面。
// javascript
var MyLinkedList = function() {
this.head = new ListNode(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.head;
while (index >= 0) {
index--;
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;
// 因为有哨兵节点,所以 0 <= index <= this.size 都能找到前驱节点
const node = new ListNode(val);
let cur = this.head;
while (index - 1 >= 0) {
index--;
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
this.size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) return;
// 因为有哨兵节点,所以 0 <= index < this.size 都能找到前驱节点
let cur = this.head;
while (index - 1 >= 0) {
index--;
cur = cur.next;
}
cur.next = cur.next.next;
this.size--;
};
没有哨兵节点:
// javascript
var MyLinkedList = function() {
this.head = null;
this.size = 0;
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) return -1;
let cur = this.head;
while (index > 0) {
index--;
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;
const node = new ListNode(val);
if (index === 0) {
if (this.size === 0) this.head = node;
else {
node.next = this.head;
this.head = node;
}
} else {
let cur = this.head;
while (index - 1 > 0) {
index--;
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
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 {
let cur = this.head;
while (index - 1 > 0) {
index--;
cur = cur.next;
}
cur.next = cur.next.next;
}
this.size--;
};
解题二:双向链表
// javascript
var DoublyListNode = function(val, prev, next) {
this.val = (val === undefined) ? 0 : val;
this.prev = (prev === undefined) ? null : prev;
this.next = (next === undefined) ? null : next;
};
var MyLinkedList = function() {
this.head = null;
this.tail = null;
this.size = 0;
};
MyLinkedList.prototype.getNode = function(index) {
let cur = null;
if (index < (this.size >> 1)) {
cur = this.head;
for (let i = 0; i < index; ++i) cur = cur.next;
} else {
cur = this.tail;
for (let i = this.size - 1; i > index; --i) {
cur = cur.prev;
}
}
return cur;
}
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) return -1;
const node = this.getNode(index);
return node.val;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
const prevHead = this.head;
this.head = new DoublyListNode(val);
this.head.next = prevHead;
if (prevHead !== null) {
prevHead.prev = this.head;
} else {
this.tail = this.head; // 旧的head是null代表原先没有任何元素
}
this.size++;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
const prevTail = this.tail;
this.tail = new DoublyListNode(val);
this.tail.prev = prevTail;
if (prevTail !== null) {
prevTail.next = this.tail;
} else {
this.head = this.tail; // 旧的tail是null代表原先没有任何元素
}
this.size++;
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index < 0 || index > this.size) return;
if (index === 0) return this.addAtHead(val);
if (index === this.size) return this.addAtTail(val);
const curNode = new DoublyListNode(val);
const nextNode = this.getNode(index);
const prevNode = nextNode.prev;
curNode.next = nextNode;
curNode.prev = prevNode;
prevNode.next = curNode;
nextNode.prev = curNode;
this.size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) return;
// this.size = 1时,如果 index = 0, this.size - 1 = 0
// 所以 this.size === 1 的处理放在 if (index === 0) / if (index === this.size - 1) 都行
if (index === 0) {
if (this.size === 1) {
this.head = this.tail = null;
} else {
this.head = this.head.next;
this.head.prev = null;
}
} else if (index === this.size - 1) {
this.tail = this.tail.prev;
this.tail.next = null;
} else {
const curNode = this.getNode(index);
const prevNode = curNode.prev, nextNode = curNode.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
// curNode.prev.next = curNode.next;
// curNode.next.prev = curNode.prev;
// curNode.prev = curNode.next = null; // 可加可不加
}
this.size--;
};