|
| 1 | +/** |
| 2 | + * 707. Design Linked List |
| 3 | + * https://leetcode.com/problems/design-linked-list/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * Design your implementation of the linked list. You can choose to use a singly or |
| 7 | + * doubly linked list. |
| 8 | + * |
| 9 | + * A node in a singly linked list should have two attributes: val and next. val is the value |
| 10 | + * of the current node, and next is a pointer/reference to the next node. |
| 11 | + * |
| 12 | + * If you want to use the doubly linked list, you will need one more attribute prev to indicate |
| 13 | + * the previous node in the linked list. Assume all nodes in the linked list are 0-indexed. |
| 14 | + * |
| 15 | + * Implement the MyLinkedList class: |
| 16 | + * - MyLinkedList() Initializes the MyLinkedList object. |
| 17 | + * - int get(int index) Get the value of the indexth node in the linked list. If the |
| 18 | + * index is invalid, return -1. |
| 19 | + * - void addAtHead(int val) Add a node of value val before the first element of the |
| 20 | + * linked list. After the insertion, the new node will be the first node of the linked list. |
| 21 | + * - void addAtTail(int val) Append a node of value val as the last element of the linked list. |
| 22 | + * - void addAtIndex(int index, int val) Add a node of value val before the indexth node |
| 23 | + * in the linked list. If index equals the length of the linked list, the node will be |
| 24 | + * appended to the end of the linked list. If index is greater than the length, the node |
| 25 | + * will not be inserted. |
| 26 | + * - void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index |
| 27 | + * is valid. |
| 28 | + */ |
| 29 | + |
| 30 | +var MyLinkedList = function() { |
| 31 | + this.head = null; |
| 32 | + this.size = 0; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * @param {number} index |
| 37 | + * @return {number} |
| 38 | + */ |
| 39 | +MyLinkedList.prototype.get = function(index) { |
| 40 | + if (index < 0 || index >= this.size) return -1; |
| 41 | + let current = this.head; |
| 42 | + while (index--) current = current.next; |
| 43 | + return current.val; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * @param {number} val |
| 48 | + * @return {void} |
| 49 | + */ |
| 50 | +MyLinkedList.prototype.addAtHead = function(val) { |
| 51 | + const node = new Node(val); |
| 52 | + node.next = this.head; |
| 53 | + this.head = node; |
| 54 | + this.size++; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * @param {number} val |
| 59 | + * @return {void} |
| 60 | + */ |
| 61 | +MyLinkedList.prototype.addAtTail = function(val) { |
| 62 | + const node = new Node(val); |
| 63 | + if (!this.head) { |
| 64 | + this.head = node; |
| 65 | + } else { |
| 66 | + let current = this.head; |
| 67 | + while (current.next) current = current.next; |
| 68 | + current.next = node; |
| 69 | + } |
| 70 | + this.size++; |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * @param {number} index |
| 75 | + * @param {number} val |
| 76 | + * @return {void} |
| 77 | + */ |
| 78 | +MyLinkedList.prototype.addAtIndex = function(index, val) { |
| 79 | + if (index < 0 || index > this.size) return; |
| 80 | + if (index === 0) return this.addAtHead(val); |
| 81 | + const node = new Node(val); |
| 82 | + let current = this.head; |
| 83 | + while (--index) current = current.next; |
| 84 | + node.next = current.next; |
| 85 | + current.next = node; |
| 86 | + this.size++; |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * @param {number} index |
| 91 | + * @return {void} |
| 92 | + */ |
| 93 | +MyLinkedList.prototype.deleteAtIndex = function(index) { |
| 94 | + if (index < 0 || index >= this.size) return; |
| 95 | + this.size--; |
| 96 | + if (index === 0) { |
| 97 | + this.head = this.head.next; |
| 98 | + return; |
| 99 | + } |
| 100 | + let current = this.head; |
| 101 | + while (--index) current = current.next; |
| 102 | + current.next = current.next.next; |
| 103 | +}; |
| 104 | + |
| 105 | +class Node { |
| 106 | + constructor(val) { |
| 107 | + this.val = val; |
| 108 | + this.next = null; |
| 109 | + } |
| 110 | +} |
0 commit comments