Skip to content

Commit 55c902a

Browse files
2 parents 606a2bc + aacb13f commit 55c902a

File tree

6 files changed

+162
-4
lines changed

6 files changed

+162
-4
lines changed

FE-JS-Questions/Carousel.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</div>
2222

2323
<button class="carouselButton carouselButtonRight">
24-
<img src="images/left.svg" alt="" />
24+
<img src="images/right.svg" alt="" />
2525
</button>
2626

2727
<div class="carouselNavigation">
@@ -195,4 +195,4 @@ dotsNav.addEventListener('click', event => {
195195
updateDots(currentDot, targetDot);
196196
hideShowArrows(targetIndex);
197197
})
198-
```
198+
```

exercises/leetcode/amazon/copyListWithRandomPointer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const copyRandomList = head => {
3535
}
3636

3737
if (visited.has(node)) {
38-
visited.get(node);
38+
return visited.get(node);
3939
}
4040

4141
visited.set(node, new Node(node.val, node.next, node.random));

exercises/leetcode/cloneGraph.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Given a reference of a node in a connected undirected graph,
3+
* return a deep copy (clone) of the graph.
4+
* Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.
5+
*/
6+
/**
7+
* // Definition for a Node.
8+
* function Node(val,neighbors) {
9+
* this.val = val;
10+
* this.neighbors = neighbors;
11+
* };
12+
*/
13+
/**
14+
* @param {Node} node
15+
* @return {Node}
16+
*/
17+
const visited = new Map();
18+
const cloneGraph = node => {
19+
if (node === null) {
20+
return node;
21+
}
22+
23+
if (visited.has(node)) {
24+
return visited.get(node);
25+
}
26+
27+
let cloneNode = new Node(node, []);
28+
visited.put(node, cloneNode);
29+
30+
for (let neighbor of node.neighbors) {
31+
cloneNode.neighbors.push(cloneGraph(neighbor));
32+
}
33+
34+
return cloneNode;
35+
}
36+
37+
/**
38+
* Time Complexity: O(N) (As we process each node once)
39+
* Space Complexity: O(N) (Space taken by the map -> visited, and also the space taken up
40+
* by the height of the recursion stack O(H))
41+
*/
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
3+
4+
Example:
5+
6+
Input:
7+
[
8+
1->4->5,
9+
1->3->4,
10+
2->6
11+
]
12+
Output: 1->1->2->3->4->4->5->6
13+
*/
14+
/**
15+
* Definition for singly-linked list. */
16+
function ListNode(val) {
17+
this.val = val;
18+
this.next = null;
19+
}
20+
21+
22+
const mergeKLists = lists => {
23+
let head = new ListNode();
24+
let point = head;
25+
let nodes = [];
26+
27+
for (let l of lists) {
28+
while (l) {
29+
nodes.push(l.val);
30+
l = l.next;
31+
}
32+
}
33+
nodes.sort((a, b) => a - b);
34+
for (let n of nodes) {
35+
point.next = new ListNode(n);
36+
point = point.next;
37+
}
38+
39+
return head.next;
40+
}
41+
42+
// Time Complexity: O (NlogN) => O(N) for looping list and creating array
43+
// O(NlogN) for sorting
44+
// O(N) for creating new list
45+
// Space Complexity: O(N)
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* You are given two non-empty linked lists representing two non-negative integers.
3+
* The most significant digit comes first and each of their nodes contain a single digit.
4+
* Add the two numbers and return it as a linked list.
5+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
7+
Follow up:
8+
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
9+
10+
Example:
11+
12+
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 8 -> 0 -> 7
14+
*/
15+
/**
16+
* Definition for singly-linked list.
17+
* function ListNode(val) {
18+
* this.val = val;
19+
* this.next = null;
20+
* }
21+
*/
22+
/**
23+
* @param {ListNode} l1
24+
* @param {ListNode} l2
25+
* @return {ListNode}
26+
*/
27+
const addTwoNumbers = (l1, l2) => {
28+
const s1 = [];
29+
let node1 = l1;
30+
31+
while (node1) {
32+
s1.push(node1);
33+
node1 = node1.next;
34+
}
35+
36+
const s2 = [];
37+
let node2 = l2;
38+
39+
while (node2) {
40+
s2.push(node2);
41+
node2 = node2.next;
42+
}
43+
44+
let carry = 0;
45+
let prev = null;
46+
let result = 0;
47+
48+
while (s1.length > 0 || s2.length > 0) {
49+
let num1 = s1.length > 0 ? s1.pop().val : 0;
50+
let num2 = s2.length > 0 ? s2.pop().val : 0;
51+
52+
result = num1 + num2 + carry;
53+
if (result > 9) {
54+
result = result - 10;
55+
carry = 1;
56+
} else {
57+
carry = 0;
58+
}
59+
60+
let curr = new ListNode(result);
61+
curr.next = prev;
62+
prev = curr;
63+
}
64+
65+
if (carry) {
66+
let curr = new ListNode(carry);
67+
curr.next = prev;
68+
prev = curr;
69+
}
70+
71+
return prev;
72+
}

exercises/stacks&queues/queue.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class Queue {
2525

2626
// Return the last element in the queue without popping
2727
peek() {
28-
return this.data[this.data.length - 1];
28+
return this.data[0];
2929
}
3030
}

0 commit comments

Comments
 (0)