leetcode 算法题707 (简单184) 设计链表
- 题目介绍
设计链表的实现。
您可以选择使用单链表或双链表。
单链表中的节点应该具有两个属性:val 和 next。
val 是当前节点的值,next 是指向下一个节点的指针/引用。
如果要使用双向链表,
则还需要一个属性 prev 以指示链表中的上一个节点。
假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
get(index):
获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):
在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):
将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):
在链表中的第 index 个节点之前添加值为 val 的节点。
如果 index 等于链表的长度,则该节点将附加到链表的末尾。
如果 index 大于链表长度,则不会插入节点。
如果index小于0,则在头部插入节点。
deleteAtIndex(index):
如果索引 index 有效,则删除链表中的第 index 个节点。
- 示例
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3
- 注意
所有val值都在 [1, 1000] 之内。
操作次数将在 [1, 1000] 之内。
请不要使用内置的 LinkedList 库。
- 解法一
/**
* Initialize your data structure here.
*/
var MyLinkedList = function() {
this.header = null;
this.end = null;
this.length = 0;
};
MyLinkedList.prototype.get = function(index) {
let node = this.getNode(index);
if(node) {
return node.val;
}
return -1;
};
/**
* Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
let temp = this.newInstance(val);
let header = this.header;
if(header) {
temp.next = header;
header.pre = temp;
} else {
this.end = temp;
}
this.header = temp;
this.length++;
};
/**
* Append a node of value val to the last element of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
let temp = this.newInstance(val);
let end = this.end;
if(end) {
end.next = temp;
temp.pre = end;
} else {
this.header = end;
}
this.end = temp;
this.length++;
};
/**
* Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
console.log(this.header)
let length = this.length;
if(index > length ) {
return;
}
if(index <= 0) {
this.addAtHead(val);
} else if(index === length) {
this.addAtTail(val);
} else {
let temp = this.getNode(index), node = this.newInstance(val);
node.next = temp;
node.pre = temp.pre;
temp.pre.next = node;
temp.pre = node;
this.length++;
}
};
/**
* Delete the index-th node in the linked list, if the index is valid.
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
let length = this.length;
if(index >= length || index < 0) {
return
}
if(index === 0) {
this.header = this.header.next;
if(this.header) {
this.header.pre = null
} else {
this.end = null;
}
} else if(index === length - 1) {
this.end = this.end.pre;
if(this.end) {
this.end.next = null;
} else {
this.header = null;
}
} else {
let temp = this.getNode(index);
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}
this.length--;
};
/**
* Get the value of the index-th node in the linked list. If the index is invalid, return -1.
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.getNode = function(index) {
let length = this.length;
if(index >= length || index < 0) {
return null;
}
let node;
if(index >= length / 2) {
node = this.end;
let i = length - 1;
console.log(i, length, index, node)
while(i > index) {
node = node.pre;
i--;
}
} else {
node = this.header;
let i = 0;
while(i < index) {
node = node.next;
i++;
}
}
return node;
};
MyLinkedList.prototype.newInstance = function(val) {
return {
pre: null,
next: null,
val
};
};
/**
* 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)
*/
执行用时 : 296 ms, 在所有 JavaScript 提交中击败了5.80%的用户
内存消耗 : 45.9 MB, 在所有 JavaScript 提交中击败了5.80%的用户
- 解法二
/**
* Initialize your data structure here.
*/
var MyLinkedList = function() {
this.list = [];
};
MyLinkedList.prototype.get = function(index) {
if(index >= 0 && index < this.list.length) {
return this.list[index];
}
return -1;
};
/**
* Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
this.list.unshift(val);
};
/**
* Append a node of value val to the last element of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
this.list.push(val);
};
/**
* Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if(index <= 0) {
return this.addAtHead(val);
}
if(index <= this.list.length) {
this.list.splice(index, 0, val);
}
};
/**
* Delete the index-th node in the linked list, if the index is valid.
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if(index < 0 || index >= this.list.length) {
return;
}
this.list.splice(index, 1);
};
执行用时 : 136 ms, 在所有 JavaScript 提交中击败了99.55%的用户
内存消耗 : 42.5 MB, 在所有 JavaScript 提交中击败了21.57%的用户