LeetCode: Design Linked List Solution
Implement some linked list methodsImplementation
1var Node = function (val) {2 this.val = val3 this.next = null4}56/**7 * Initialize your data structure here.8 */9var MyLinkedList = function () {10 this.head = null11 this.size = 012}1314/**15 * Get the value of the index-th node in the linked list. If the index is invalid, return -1.16 * @param {number} index17 * @return {number}18 */19MyLinkedList.prototype.get = function (index) {20 if (index < 0 || index >= this.size || this.head === null) {21 return -122 }2324 let curr = this.head25 while (index--) {26 curr = curr.next27 }2829 return curr.val30}3132/**33 * 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.34 * @param {number} val35 * @return {void}36 */37MyLinkedList.prototype.addAtHead = function (val) {38 const node = new Node(val)39 node.next = this.head40 this.head = node41 this.size++42}4344/**45 * Append a node of value val to the last element of the linked list.46 * @param {number} val47 * @return {void}48 */49MyLinkedList.prototype.addAtTail = function (val) {50 if (this.head === null) {51 this.addAtHead(val)52 return53 }5455 const node = new Node(val)56 let curr = this.head57 while (curr.next !== null) {58 curr = curr.next59 }60 curr.next = node61 this.size++62}6364/**65 * 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.66 * @param {number} index67 * @param {number} val68 * @return {void}69 */70MyLinkedList.prototype.addAtIndex = function (index, val) {71 if (index === 0) {72 this.addAtHead(val)73 return74 }75 if (index === this.size) {76 this.addAtTail(val)77 return78 }79 if (index < 0 || index > this.size) {80 return81 }8283 const node = new Node(val)84 let curr = this.head85 while (index - 1) {86 curr = curr.next87 index--88 }89 node.next = curr.next90 curr.next = node91 this.size++92}9394/**95 * Delete the index-th node in the linked list, if the index is valid.96 * @param {number} index97 * @return {void}98 */99MyLinkedList.prototype.deleteAtIndex = function (index) {100 if (index < 0 || index >= this.size) {101 return102 }103104 let curr = this.head105 if (index === 0) {106 this.head = curr.next107 } else {108 while (index - 1) {109 curr = curr.next110 index--111 }112 curr.next = curr.next.next113 }114 this.size--115}116117/**118 * Your MyLinkedList object will be instantiated and called as such:119 * var obj = new MyLinkedList()120 * var param_1 = obj.get(index)121 * obj.addAtHead(val)122 * obj.addAtTail(val)123 * obj.addAtIndex(index,val)124 * obj.deleteAtIndex(index)125 */
Running test cases locally
1const methods = [2 "addAtHead",3 "addAtTail",4 "addAtIndex",5 "get",6 "deleteAtIndex",7 "get",8]9const params = [[1], [3], [1, 2], [1], [1], [1]]10var obj = new MyLinkedList()1112for (let i = 0; i < methods.length; i++) {13 console.log(methods[i], params[i])14 console.log(obj[methods[i]].apply(obj, params[i]))1516 const arr = []17 let curr = obj.head18 while (curr) {19 arr.push(curr.val)20 curr = curr.next21 }22 console.log("//", arr)23 console.log()24}
Comments
Loading comments...
Tags
leetcode
linked list
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Search Insert Position
Understand why returns "low/left"
Previous Post
LeetCode: Valid Parentheses
Classic stack problem