Skip to content

[Evan] Week 2 Solutions #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions invert-binary-tree/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
if (!root) {
return null;
}

const originLeft = root.left;
const originRight = root.right;

root.right = invertTree(originLeft);
root.left = invertTree(originRight);

return root;
};
27 changes: 27 additions & 0 deletions linked-list-cycle/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
let fastPointer = head;
let slowPointer = head;

while (fastPointer !== null && slowPointer !== null) {
fastPointer = fastPointer?.next?.next;
slowPointer = slowPointer?.next;
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS?. 연산자 통해서 깔끔하게 적을 수 있네요 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네네ㅎㅎ Optional chaining이라는 연산자입니다. 비교적 최근에 나온 스펙이죠...


if (fastPointer === slowPointer) {
return true;
}
}

return false;
};
30 changes: 30 additions & 0 deletions merge-two-sorted-lists/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}

/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function (list1, list2) {
const dummy = new ListNode();
let current = dummy;

while (list1 !== null && list2 !== null) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}

current = current.next;
}

current.next = list1 !== null ? list1 : list2;

return dummy.next;
};
25 changes: 25 additions & 0 deletions reverse-linked-list/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function (head) {
let currentNode = head;
let previousNode = null;

while (currentNode !== null) {
const nextNode = currentNode.next;

currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}

return previousNode;
};
26 changes: 26 additions & 0 deletions valid-parentheses/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
if (s.length % 2 !== 0) {
return false;
}

const stack = [];
const pair = {
")": "(",
"}": "{",
"]": "[",
};

for (let char of s) {
if (pair[char] && stack.pop() !== pair[char]) {
return false;
}

stack.push(char);
}

return stack.length === 0;
};