Skip to content

Commit 1d29e11

Browse files
authored
Merge pull request DaleStudy#59 from sounmind/main
[Evan] Week 2 Solutions
2 parents b1f1bb2 + f9d9929 commit 1d29e11

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

invert-binary-tree/evan.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function TreeNode(val, left, right) {
2+
this.val = val === undefined ? 0 : val;
3+
this.left = left === undefined ? null : left;
4+
this.right = right === undefined ? null : right;
5+
}
6+
/**
7+
* @param {TreeNode} root
8+
* @return {TreeNode}
9+
*/
10+
var invertTree = function (root) {
11+
if (!root) {
12+
return null;
13+
}
14+
15+
const originLeft = root.left;
16+
const originRight = root.right;
17+
18+
root.right = invertTree(originLeft);
19+
root.left = invertTree(originRight);
20+
21+
return root;
22+
};

linked-list-cycle/evan.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function (head) {
14+
let fastPointer = head;
15+
let slowPointer = head;
16+
17+
while (fastPointer !== null && slowPointer !== null) {
18+
fastPointer = fastPointer?.next?.next;
19+
slowPointer = slowPointer?.next;
20+
21+
if (fastPointer === slowPointer) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
};

merge-two-sorted-lists/evan.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function ListNode(val, next) {
2+
this.val = val === undefined ? 0 : val;
3+
this.next = next === undefined ? null : next;
4+
}
5+
6+
/**
7+
* @param {ListNode} list1
8+
* @param {ListNode} list2
9+
* @return {ListNode}
10+
*/
11+
var mergeTwoLists = function (list1, list2) {
12+
const dummy = new ListNode();
13+
let current = dummy;
14+
15+
while (list1 !== null && list2 !== null) {
16+
if (list1.val < list2.val) {
17+
current.next = list1;
18+
list1 = list1.next;
19+
} else {
20+
current.next = list2;
21+
list2 = list2.next;
22+
}
23+
24+
current = current.next;
25+
}
26+
27+
current.next = list1 !== null ? list1 : list2;
28+
29+
return dummy.next;
30+
};

reverse-linked-list/evan.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function (head) {
13+
let currentNode = head;
14+
let previousNode = null;
15+
16+
while (currentNode !== null) {
17+
const nextNode = currentNode.next;
18+
19+
currentNode.next = previousNode;
20+
previousNode = currentNode;
21+
currentNode = nextNode;
22+
}
23+
24+
return previousNode;
25+
};

valid-parentheses/evan.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
if (s.length % 2 !== 0) {
7+
return false;
8+
}
9+
10+
const stack = [];
11+
const pair = {
12+
")": "(",
13+
"}": "{",
14+
"]": "[",
15+
};
16+
17+
for (let char of s) {
18+
if (pair[char] && stack.pop() !== pair[char]) {
19+
return false;
20+
}
21+
22+
stack.push(char);
23+
}
24+
25+
return stack.length === 0;
26+
};

0 commit comments

Comments
 (0)