-
Notifications
You must be signed in to change notification settings - Fork 441
/
linkedList.js
170 lines (112 loc) · 3.92 KB
/
linkedList.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
LINKED LIST
Comprised of nodes that represent a sequence.
Each node is composed of data and a reference/link to the next node.
*** Operations:
** Part 1
myList.forEach(callbackFn)
invoke callback function with the value of each node
myList.print()
=> string with all values in list (ex: '0, 1, 2, 3')
myList.insertAfter(refNode, value)
=> new node
insert new node associated with value passed in after refNode
myList.removeAfter(refNode)
=> removed node
remove node after the refNode
myList.insertHead(value)
=> new head
insert new head node at the beginning of the list with the value passed in
myList.removeHead()
=> removed head node
remove the head node of the linked list
myList.findNode(value)
=> first node that has a value matching what was passed in
* Optimization:
Say we have a linked list that has 100 items and we want to add an item to the very end. How would you do that with your current implementation? How can you modify the data structure to add an item to the end in constant time?
myList.appendToTail(value)
=> new tail node
add a new tail node at the end of the list with the associated value passed in
myList.removeTail()
=> removed tail node
remove the tail node from the list
** Part 2
Now let's think about creating insertBefore and removeBefore methods for the nodes in our list. Can you think of an efficient way to do so?
Think about time complexity. What would it be for your current implementation of a linked list?
How can we modify our data structures (Node and Linked List classes) so that we can make these O(1) operations?
Once you've come up with a plan, implement the following methods.
myList.insertBefore(refNode, value)
=> new node inserted
insert new node with associated value before refNode
myList.removeBefore(refNode)
=> removed node
remove node before the refNode passed in
*** Additional Exercises:
Implement a circularly linked list:
https://en.wikipedia.org/wiki/Linked_list#Circularly_linked_list
Reimplement stack and queue data structures using linked lists.
*/
// PART 1
function Node(value) {
this.next = null;
this.value = value;
}
function LinkedList(headValue) {
if (headValue === undefined) console.log('Must provide value for first node');
this.head = new Node(headValue);
}
LinkedList.prototype.forEach = function(callback) {
// implement me...
};
// Time complexity:
LinkedList.prototype.print = function() {
// implement me...
};
// Time complexity:
LinkedList.prototype.insertAfter = function(node, value) {
// implement me...
};
// Time complexity:
LinkedList.prototype.removeAfter = function(node) {
// implement me...
};
// Time complexity:
LinkedList.prototype.insertHead = function(value) {
// implement me...
};
// Time complexity:
LinkedList.prototype.removeHead = function() {
// implement me...
}
LinkedList.prototype.findNode = function(value) {
// implement me...
};
// Time complexity:
LinkedList.prototype.appendToTail = function(value) {
// implement me...
};
// Time complexity:
// PART 2:
LinkedList.prototype.insertBefore = function(node, value) {
// implement me...
};
// Time complexity:
LinkedList.prototype.removeBefore = function(node) {
// implement me...
};
// Time complexity:
/*
*** Exercises:
1. Implement a stack using a linked list.
2. Implement a queue using a linked list.
3. Write a method that remove duplicates from an unsorted linked list. What is the time complexity? Re-implement the method without using any additional storage structure (constant space complexity). What is the time complexity?
4. Reverse a linked list. Do not use any additional storage structures.
5. Find the kth to last element of a singly linked list.
6. Detect if a linked list has a loop.
7. Check if a linked list is a palindrome.
8. Given two linked lists that represent numbers, return a linked list that represents the sum of those numbers:
4 2 5 (4 -> 2 -> 5)
+ 7 3 1 (7 -> 3 -> 1)
--------
1 1 5 6 (1 -> 1 -> 5 -> 6)
*/