
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val
and next
. val
is the value of the current node, and next
is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev
to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement the MyLinkedList
class:
MyLinkedList()
Initializes theMyLinkedList
object.int get(int index)
Get the value of theindexth
node in the linked list. If the index is invalid, return-1
.void addAtHead(int val)
Add a node of valueval
before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.void addAtTail(int val)
Append a node of valueval
as the last element of the linked list.void addAtIndex(int index, int val)
Add a node of valueval
before theindexth
node in the linked list. Ifindex
equals the length of the linked list, the node will be appended to the end of the linked list. Ifindex
is greater than the length, the node will not be inserted.void deleteAtIndex(int index)
Delete theindexth
node in the linked list, if the index is valid.
Example 1:
Input ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] [[], [1], [3], [1, 2], [1], [1], [1]] Output
[null, null, null, null, 2, null, 3]
Explanation MyLinkedList myLinkedList = new MyLinkedList(); myLinkedList.addAtHead(1); myLinkedList.addAtTail(3); myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3 myLinkedList.get(1); // return 2 myLinkedList.deleteAtIndex(1); // now the linked list is 1->3 myLinkedList.get(1); // return 3
Constraints:
0 <= index, val <= 1000
- Please do not use the built-in LinkedList library.
- At most
2000
calls will be made toget
,addAtHead
,addAtTail
,addAtIndex
anddeleteAtIndex
.
const Node = function (val) { this.val = val this.next = null } var MyLinkedList = function() { this.head = null this.tail = null this.size = 0 }; /** * @param {number} index * @return {number} */ MyLinkedList.prototype.get = function(index) { if (this.size === 0 || index < 0 || index > this.size - 1) { return -1 } let target = this.head let i = 0 while(i < index) { target = target.next i++ } return target.val }; /** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtHead = function(val) { const node = new Node(val) if (!this.size) { this.head = node this.tail = node } else { node.next = this.head this.head = node } this.size++ }; /** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtTail = function(val) { const node = new Node(val) if (!this.size) { this.head = node this.tail = node } else { this.tail.next = node this.tail = node } 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 === this.size) { this.addAtTail(val) return } if (index === 0) { this.addAtHead(val) return } let target = this.head let i = 0 while(i < index - 1) { i++ target = target.next } const node = new Node(val) node.next = target.next ? target.next : null target.next = node this.size++ }; /** * @param {number} index * @return {void} */ MyLinkedList.prototype.deleteAtIndex = function(index) { if (index < 0 || index > this.size - 1) { return } if(index === 0) { this.head = this.head.next this.size-- return } let i = 0 let target = this.head while(i < index - 1) { i++ target = target.next } target.next = target.next.next if (!target.next) { this.tail = target } 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) */
第一次觸碰時,花了不少時間去搞懂 LinkedList。 弄懂之後,發現概念上其實沒那麼複雜。
首先必須了解這個資料結構的基礎原理,如下圖:

在 js 中,必須要有一個 Node 的模型,和一個用來整合的 LinkedList 模型。
// 基本的 Node 模型 const Node = function (val) { this.val = val this.next = null }
而 LinkedList 則是用來整合,記錄這些節點,通常我們只需要知道起始節點(head)就可以做計算。
這題為了計算方便,增加了 size (節點數量) 和 tail (結束節點)
var MyLinkedList = function() { this.head = null this.tail = null this.size = 0 };
增加 tail 有一個明顯的好處就是在處理增加結尾節點(addAtTail)時,可以不用從 head 開始往下找。
MyLinkedList.prototype.addAtTail = function(val) { const node = new Node(val) if (!this.size) { this.head = node this.tail = node } else { this.tail.next = node this.tail = node } this.size++ };
選取節點時,通常使用一個 loop 去找到我們要選取的目標
MyLinkedList.prototype.get = function(index) { if (this.size === 0 || index < 0 || index > this.size - 1) { return -1 } let target = this.head let i = 0 while(i < index) { target = target.next i++ } return target.val };
插入和刪除時,就會發現在 LinkedList 增加 size 的好處,可以直接避免 input value 的錯誤。
MyLinkedList.prototype.addAtIndex = function(index, val) { if (index < 0 || index > this.size) { return } if (index === this.size) { this.addAtTail(val) return } if (index === 0) { this.addAtHead(val) return } let target = this.head let i = 0 while(i < index - 1) { i++ target = target.next } const node = new Node(val) node.next = target.next ? target.next : null target.next = node this.size++ }; /** * @param {number} index * @return {void} */ MyLinkedList.prototype.deleteAtIndex = function(index) { if (index < 0 || index > this.size - 1) { return } if(index === 0) { this.head = this.head.next this.size-- return } let i = 0 let target = this.head while(i < index - 1) { i++ target = target.next } target.next = target.next.next if (!target.next) { this.tail = target } this.size-- };
到這裡,基礎的 LinkedList 觀念已經差不多有個雛形了。