Skip to content

Commit 32ba74a

Browse files
Create Design Linked List.md
1 parent 4dad249 commit 32ba74a

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

JS-Algo/Design Linked List.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#### Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
2+
#### 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.
3+
#### 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.
4+
5+
<img width="480" alt="Screen Shot 2021-11-10 at 19 49 45" src="https://user-images.githubusercontent.com/37787994/141228655-876ef1fc-eea5-4d3d-b529-a18f9c6bf394.png">
6+
7+
<img width="464" alt="Screen Shot 2021-11-10 at 19 50 03" src="https://user-images.githubusercontent.com/37787994/141228689-b56d6ce1-69bb-4d37-9e68-bdb7807b2c12.png">
8+
9+
10+
```js
11+
class ListNode {
12+
constructor(val, next) {
13+
this.val = val == undefined ? 0 : val;
14+
this.next = next == undefined ? null : next;
15+
}
16+
}
17+
18+
/**
19+
* Initialize your data structure here.
20+
*/
21+
var MyLinkedList = function() {
22+
this.head = null;
23+
};
24+
25+
/**
26+
* Get the value of the index-th node in the linked list. If the index is invalid, return -1.
27+
* @param {number} index
28+
* @return {number}
29+
*/
30+
MyLinkedList.prototype.get = function(index) {
31+
if (index < 0 || index >= this.getLength()) return -1;
32+
let cur = this.head;
33+
for (let i = 0; i < index; i++) {
34+
cur = cur.next;
35+
}
36+
return cur.val;
37+
};
38+
39+
/**
40+
* 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.
41+
* @param {number} val
42+
* @return {void}
43+
*/
44+
MyLinkedList.prototype.addAtHead = function(val) {
45+
let node = new ListNode(val);
46+
node.next = this.head;
47+
this.head = node;
48+
};
49+
50+
/**
51+
* Append a node of value val to the last element of the linked list.
52+
* @param {number} val
53+
* @return {void}
54+
*/
55+
MyLinkedList.prototype.addAtTail = function(val) {
56+
if (this.head == null) {
57+
this.addAtHead(val);
58+
return;
59+
}
60+
let node = new ListNode(val);
61+
let cur = this.head;
62+
while (cur.next !== null) {
63+
cur = cur.next;
64+
}
65+
cur.next = node;
66+
};
67+
68+
/**
69+
* 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.
70+
* @param {number} index
71+
* @param {number} val
72+
* @return {void}
73+
*/
74+
MyLinkedList.prototype.addAtIndex = function(index, val) {
75+
if (index === 0) {
76+
this.addAtHead(val);
77+
return;
78+
}
79+
if (index === this.getLength()) {
80+
this.addAtTail(val);
81+
return;
82+
}
83+
if (index > this.getLength()) return;
84+
85+
let cur = this.head;
86+
let node = new ListNode(val);
87+
for (let i = 0; i < index-1; i++) {
88+
cur = cur.next;
89+
}
90+
let next = cur.next;
91+
cur.next = node;
92+
node.next = next;
93+
};
94+
95+
/**
96+
* Delete the index-th node in the linked list, if the index is valid.
97+
* @param {number} index
98+
* @return {void}
99+
*/
100+
MyLinkedList.prototype.deleteAtIndex = function(index) {
101+
if (index < 0 || index >= this.getLength()) return;
102+
if (index == 0) {
103+
this.head = this.head.next;
104+
return;
105+
}
106+
107+
let cur = this.head;
108+
for (let i = 0; i < index-1; i++) {
109+
cur = cur.next;
110+
}
111+
cur.next = cur.next.next;
112+
};
113+
114+
MyLinkedList.prototype.getLength = function() {
115+
let len = 0, cur = this.head;
116+
while (cur !== null) {
117+
cur = cur.next;
118+
len++;
119+
}
120+
return len;
121+
};
122+
```

0 commit comments

Comments
 (0)