MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / addAtIndex

Method addAtIndex

javascript/0707-design-linked-list.js:46–65  ·  view source on GitHub ↗
(index, val)

Source from the content-addressed store, hash-verified

44 }
45
46 addAtIndex(index, val) {
47 if (index === 0) this.addAtHead(val);
48 else {
49 let curr = this.head;
50 while (curr && index > 0) {
51 curr = curr.next;
52 index--;
53 }
54 if (index === 0) {
55 if (!curr) this.addAtTail(val);
56 else {
57 const prev = curr.prev;
58 prev.next = new Node(val);
59 prev.next.prev = prev;
60 prev.next.next = curr;
61 curr.prev = prev.next;
62 }
63 }
64 }
65 }
66
67 deleteAtIndex(index) {
68 if (!this.head) return;

Callers

nothing calls this directly

Calls 2

addAtHeadMethod · 0.95
addAtTailMethod · 0.95

Tested by

no test coverage detected