diff --git a/invert-binary-tree/nhistory.js b/invert-binary-tree/nhistory.js new file mode 100644 index 000000000..e802fa36a --- /dev/null +++ b/invert-binary-tree/nhistory.js @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * 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) { + // Check root is null + if (!root) return null; + // Create left and right variable to make recurrsive + let left = root.left; + let right = root.right; + // Execute invertTree functino + root.left = invertTree(right); + root.right = invertTree(left); + return root; +}; + +// TC: O(n) +// SC: O(n) diff --git a/linked-list-cycle/nhistory.js b/linked-list-cycle/nhistory.js new file mode 100644 index 000000000..68426615c --- /dev/null +++ b/linked-list-cycle/nhistory.js @@ -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) { + // Make fast pointer + // Fast pointer will move two steps further inside of list + let fast = head; + // Iterate until fast pointer and head is equal + while (fast && fast.next) { + head = head.next; + fast = fast.next.next; + if (head == fast) return true; + } + return false; +}; + +// TC: O(n) +// SC: O(1) diff --git a/merge-two-sorted-lists/nhistory.js b/merge-two-sorted-lists/nhistory.js new file mode 100644 index 000000000..6666a12ee --- /dev/null +++ b/merge-two-sorted-lists/nhistory.js @@ -0,0 +1,44 @@ +/** + * Definition for singly-linked list. + * 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) { + // Make sure both or one of the list is null + if (!list1 && !list2) return null; + if (!list1) return list2; + if (!list2) return list1; + + // Create dummy listNode + let dummy = new ListNode(0); + let head = dummy; + // Make head of dummy list by using smaller node from list1 and list2 + while (list1 && list2) { + if (list1.val <= list2.val) { + dummy.next = list1; + list1 = list1.next; + } else { + dummy.next = list2; + list2 = list2.next; + } + // After choosing with dummy list, head should be moved next + dummy = dummy.next; + } + // Iterate until both of list head is equal to null + if (list1 !== null) { + dummy.next = list1; + } else { + dummy.next = list2; + } + return head.next; +}; + +// TC: O(m+n) +// SC: O(1) diff --git a/reverse-linked-list/nhistory.js b/reverse-linked-list/nhistory.js new file mode 100644 index 000000000..f47821de6 --- /dev/null +++ b/reverse-linked-list/nhistory.js @@ -0,0 +1,63 @@ +/** + * 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} + */ + +// Declare linked list +function ListNode(val) { + this.val = val; + this.next = null; +} + +var reverseList = function (head) { + let prev = null; + let curr = head; + let next; + + while (curr !== null) { + next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + return prev; +}; + +// Create liked list nodes +const node1 = new ListNode(1); +const node2 = new ListNode(2); +const node3 = new ListNode(3); +const node4 = new ListNode(4); +const node5 = new ListNode(5); + +node1.next = node2; +node2.next = node3; +node3.next = node4; +node4.next = node5; + +// Print out linked list +let current = node1; +while (current !== null) { + console.log(current.val); + current = current.next; +} + +// Reversed linked list +const reversedHead = reverseList(node1); + +// Print out reversed linked list +current = reversedHead; +while (current !== null) { + console.log(current.val); + current = current.next; +} + +// TC: O(n) +// SC: O(1) diff --git a/valid-parentheses/nhistory.js b/valid-parentheses/nhistory.js new file mode 100644 index 000000000..9ef004939 --- /dev/null +++ b/valid-parentheses/nhistory.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function (s) { + // Create stack array + let stack = []; + // Iterate string to find there is an open bracket + for (char of s) { + // If it is an open bracket, push the close bracket into stack array + if (char === "(") stack.push(")"); + else if (char === "{") stack.push("}"); + else if (char === "[") stack.push("]"); + // If it is not an open bracket, compare last element of stack array + else if (char !== stack.pop()) return false; + } + // Check stack array still has any element after iteration + return stack.length === 0; +}; + +// TC: O(n) +// MC: O(n) + +console.log(isValid("()")); //true +console.log(isValid("()[]{}")); //true +console.log(isValid("([)]")); //false