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