Skip to content

Commit a7eb7f7

Browse files
BarklimBarklim
authored andcommitted
Create 0707.js
1 parent 82aace9 commit a7eb7f7

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

example/3.Linkedlist/0707.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
2+
var MyLinkedList = function() {
3+
4+
};
5+
6+
/**
7+
* @param {number} index
8+
* @return {number}
9+
*/
10+
MyLinkedList.prototype.get = function(index) {
11+
12+
};
13+
14+
/**
15+
* @param {number} val
16+
* @return {void}
17+
*/
18+
MyLinkedList.prototype.addAtHead = function(val) {
19+
20+
};
21+
22+
/**
23+
* @param {number} val
24+
* @return {void}
25+
*/
26+
MyLinkedList.prototype.addAtTail = function(val) {
27+
28+
};
29+
30+
/**
31+
* @param {number} index
32+
* @param {number} val
33+
* @return {void}
34+
*/
35+
MyLinkedList.prototype.addAtIndex = function(index, val) {
36+
37+
};
38+
39+
/**
40+
* @param {number} index
41+
* @return {void}
42+
*/
43+
MyLinkedList.prototype.deleteAtIndex = function(index) {
44+
45+
};
46+
47+
/**
48+
* Your MyLinkedList object will be instantiated and called as such:
49+
* var obj = new MyLinkedList()
50+
* var param_1 = obj.get(index)
51+
* obj.addAtHead(val)
52+
* obj.addAtTail(val)
53+
* obj.addAtIndex(index,val)
54+
* obj.deleteAtIndex(index)
55+
*/
56+
57+
58+
// var obj = new MyLinkedList()
59+
// var param_1 = obj.get(index)
60+
// obj.addAtHead(val)
61+
// obj.addAtTail(val)
62+
// obj.addAtIndex(index,val)
63+
// obj.deleteAtIndex(index)
64+
65+
const myLinkedList = new MyLinkedList();
66+
myLinkedList.addAtHead(1);
67+
myLinkedList.addAtTail(3);
68+
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
69+
myLinkedList.get(1); // return 2
70+
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
71+
myLinkedList.get(1); // return 3
72+
73+
console.log(myLinkedList);
74+
console.log(myLinkedList.get(1)); // 3
75+
console.log(myLinkedList.get(2)); // -1
76+
console.log(myLinkedList.get(3)); // -1
77+
78+
79+
// class Node {
80+
// constructor(val = null, prev = null, next = null) {
81+
// this.val = val
82+
// this.prev = prev
83+
// this.next = next
84+
// }
85+
// }
86+
87+
// var MyLinkedList = function() {
88+
// this.head = new Node()
89+
// this.tail = new Node()
90+
// this.length = 0
91+
// this.head.next = this.tail
92+
// this.tail.prev = this.head
93+
// };
94+
95+
// /**
96+
// * @param {number} index
97+
// * @return {number}
98+
// */
99+
// MyLinkedList.prototype.get = function(idx) {
100+
// if (idx < 0 || idx >= this.length) return -1
101+
102+
// let curNode = this.head.next
103+
104+
// while (idx--) curNode = curNode.next
105+
106+
// return curNode.val
107+
// };
108+
109+
// /**
110+
// * @param {number} val
111+
// * @return {void}
112+
// */
113+
// MyLinkedList.prototype.addAtHead = function(val) {
114+
// let prev = this.head
115+
// let next = this.head.next
116+
117+
// let node = new Node(val, prev, next)
118+
119+
// prev.next = node
120+
// next.prev = node
121+
122+
// this.length++
123+
// };
124+
125+
// /**
126+
// * @param {number} val
127+
// * @return {void}
128+
// */
129+
// MyLinkedList.prototype.addAtTail = function(val) {
130+
// let prev = this.tail.prev
131+
// let next = this.tail
132+
133+
// let node = new Node(val, prev, next)
134+
135+
// prev.next = node
136+
// next.prev = node
137+
138+
// this.length++
139+
// };
140+
141+
// /**
142+
// * @param {number} index
143+
// * @param {number} val
144+
// * @return {void}
145+
// */
146+
// MyLinkedList.prototype.addAtIndex = function(idx, val) {
147+
// if (idx < 0 || idx > this.length) return null
148+
149+
// if (idx === this.length) {
150+
// this.addAtTail(val)
151+
// return
152+
// }
153+
154+
// let prev = this.head
155+
156+
// while (idx--) prev = prev.next
157+
158+
// let next = prev.next
159+
160+
// let node = new Node(val, prev, next)
161+
162+
// prev.next = node
163+
// next.prev = node
164+
165+
// this.length++
166+
// };
167+
168+
// /**
169+
// * @param {number} index
170+
// * @return {void}
171+
// */
172+
// MyLinkedList.prototype.deleteAtIndex = function(idx) {
173+
// if (idx < 0 || idx >= this.length) return null
174+
175+
// let prev = this.head
176+
177+
// while (idx--) prev = prev.next
178+
179+
// let next = prev.next.next
180+
181+
// prev.next = next
182+
// next.prev = prev
183+
184+
// this.length--
185+
// };
186+

0 commit comments

Comments
 (0)