Skip to content

Commit 1763d2e

Browse files
committed
Add week 2 solutions
1 parent b55c5b5 commit 1763d2e

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

invert-binary-tree/yolophg.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var invertTree = function(root) {
2+
if (!root) return null;
3+
4+
// swap left and right children.
5+
let temp = root.left;
6+
root.left = root.right;
7+
root.right = temp;
8+
9+
// recursively invert left and right subtrees.
10+
invertTree(root.left);
11+
invertTree(root.right);
12+
13+
return root;
14+
};
15+
16+
// Time complexity : O(n)
17+
// Space complexity : O(n)

linked-list-cycle/yolophg.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var hasCycle = function(head) {
2+
// check to ensure that the head exists and if head is null or if there's only one, return false.
3+
if (!head || !head.next) {
4+
return false;
5+
}
6+
7+
let slow = head;
8+
let fast = head.next;
9+
10+
// iterates through the linked list looking for a cycle.
11+
while (fast && fast.next) {
12+
if (slow === fast) {
13+
return true;
14+
}
15+
// if the pointers haven't met yet, slow pointer moves one step forward.
16+
slow = slow.next;
17+
// fast pointer moves two steps forward.
18+
fast = fast.next.next;
19+
}
20+
21+
return false;
22+
};
23+
24+
// Time complexity : O(n)
25+
// Space complexity : O(1)

merge-two-sorted-lists/yolophg.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var mergeTwoLists = function(list1, list2) {
2+
// check if either of the input lists is null.
3+
if (!list1) return list2;
4+
if (!list2) return list1;
5+
6+
// create a dummy node.
7+
let dummy = new ListNode();
8+
let current = dummy;
9+
10+
// compare the values pointed to by list1 and list2.
11+
while (list1 && list2) {
12+
// append the smaller value to the merged list pointed to by current.
13+
if (list1.val < list2.val) {
14+
current.next = list1;
15+
list1 = list1.next;
16+
} else {
17+
current.next = list2;
18+
list2 = list2.next;
19+
}
20+
current = current.next;
21+
}
22+
23+
// ensure any remaining nodes are appended.
24+
current.next = list1 || list2;
25+
26+
return dummy.next;
27+
};
28+
29+
// Time complexity : O(n+m)
30+
// Space complexity : O(1)

reverse-linked-list/yolophg.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var reverseList = function(head) {
2+
let current = head;
3+
let previous = null;
4+
5+
// iterates through the linked list nodes.
6+
while(current){
7+
// stores the next node of current.
8+
const next = current.next;
9+
// reverses the direction of the pointer of current to point to the previous node.
10+
current.next = previous;
11+
// updates previous to be the current node.
12+
previous = current;
13+
// moves current to the next node.
14+
current = next;
15+
}
16+
17+
return previous;
18+
};
19+
20+
// Time complexity : O(n)
21+
// Space complexity : O(1)

valid-parentheses/yolophg.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var isValid = function(s) {
2+
let array = [];
3+
4+
// loop and store elements in the char.
5+
for(let i = 0; i < s.length; i++) {
6+
let char = s[i];
7+
8+
// if it's opening one, push a closing one in stack array.
9+
switch(char) {
10+
case "(": array.push(")");
11+
break;
12+
13+
case "{": array.push("}");
14+
break;
15+
16+
case "[": array.push("]");
17+
break;
18+
19+
default:
20+
// if it isn't opening one, pop last element and store it in the topElement.
21+
let topElement = array.pop();
22+
if(char !== topElement) {
23+
return false;
24+
}
25+
}
26+
}
27+
// check the length of array is zero.
28+
return array.length === 0;
29+
};
30+
31+
// Time complexity : O(n)
32+
// Space complexity : O(n)

0 commit comments

Comments
 (0)