# 单链表
# 实现一个链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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
个节点。
/**
* Initialize your data structure here.
*/
var MyLinkedList = function() {
this.head = null;
this.size = 0;
};
/**
* 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.get = function(index) {
if (index >= 0 && index <= this.size - 1) {
var count = 0;
var current = this.head;
var prev;
while (count < index) {
prev = current;
current = current.next;
count++;
}
return current.val;
} else {
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) {
var temp = this.head;
var newNode = {
val,
next: temp
};
this.head = newNode;
this.size++;
};
/**
* Append a node of value val to the last element of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
if (!this.head) {
this.head = {
val,
next: null
};
} else {
var tail = this.head;
while (tail.next) {
tail = tail.next;
}
tail.next = {
val,
next: null
};
}
this.size++;
};
/**
* 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) {
this.addAtHead(val);
} else if (index <= this.size - 1) {
var count = 0;
var current = this.head;
var prev;
while (count < index) {
prev = current;
current = current.next;
count++;
}
prev.next = {
val,
next: current
};
this.size++;
} else if (index === this.size) {
this.addAtTail(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.size - 1) {
if (index === 0) {
this.head = this.head.next;
this.size--;
return;
}
var count = 0;
var current = this.head;
var prev;
// 循环找到目标位置的前一个节点
while (count < index) {
prev = current;
current = current.next;
count++;
}
prev.next = current.next;
this.size--;
} else {
return null;
}
};
/**
* 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)
*/
← 栈与深度优先搜索(DFS) 双指针技巧 →