File tree Expand file tree Collapse file tree 5 files changed +186
-0
lines changed
Expand file tree Collapse file tree 5 files changed +186
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } root
11+ * @return {TreeNode }
12+ */
13+ var invertTree = function ( root ) {
14+ // Check root is null
15+ if ( ! root ) return null ;
16+ // Create left and right variable to make recurrsive
17+ let left = root . left ;
18+ let right = root . right ;
19+ // Execute invertTree functino
20+ root . left = invertTree ( right ) ;
21+ root . right = invertTree ( left ) ;
22+ return root ;
23+ } ;
24+
25+ // TC: O(n)
26+ // SC: O(n)
Original file line number Diff line number Diff line change 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+ // Make fast pointer
15+ // Fast pointer will move two steps further inside of list
16+ let fast = head ;
17+ // Iterate until fast pointer and head is equal
18+ while ( fast && fast . next ) {
19+ head = head . next ;
20+ fast = fast . next . next ;
21+ if ( head == fast ) return true ;
22+ }
23+ return false ;
24+ } ;
25+
26+ // TC: O(n)
27+ // SC: O(1)
Original file line number Diff line number Diff line change 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 } list1
10+ * @param {ListNode } list2
11+ * @return {ListNode }
12+ */
13+ var mergeTwoLists = function ( list1 , list2 ) {
14+ // Make sure both or one of the list is null
15+ if ( ! list1 && ! list2 ) return null ;
16+ if ( ! list1 ) return list2 ;
17+ if ( ! list2 ) return list1 ;
18+
19+ // Create dummy listNode
20+ let dummy = new ListNode ( 0 ) ;
21+ let head = dummy ;
22+ // Make head of dummy list by using smaller node from list1 and list2
23+ while ( list1 && list2 ) {
24+ if ( list1 . val <= list2 . val ) {
25+ dummy . next = list1 ;
26+ list1 = list1 . next ;
27+ } else {
28+ dummy . next = list2 ;
29+ list2 = list2 . next ;
30+ }
31+ // After choosing with dummy list, head should be moved next
32+ dummy = dummy . next ;
33+ }
34+ // Iterate until both of list head is equal to null
35+ if ( list1 !== null ) {
36+ dummy . next = list1 ;
37+ } else {
38+ dummy . next = list2 ;
39+ }
40+ return head . next ;
41+ } ;
42+
43+ // TC: O(m+n)
44+ // SC: O(1)
Original file line number Diff line number Diff line change 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+
13+ // Declare linked list
14+ function ListNode ( val ) {
15+ this . val = val ;
16+ this . next = null ;
17+ }
18+
19+ var reverseList = function ( head ) {
20+ let prev = null ;
21+ let curr = head ;
22+ let next ;
23+
24+ while ( curr !== null ) {
25+ next = curr . next ;
26+ curr . next = prev ;
27+ prev = curr ;
28+ curr = next ;
29+ }
30+ return prev ;
31+ } ;
32+
33+ // Create liked list nodes
34+ const node1 = new ListNode ( 1 ) ;
35+ const node2 = new ListNode ( 2 ) ;
36+ const node3 = new ListNode ( 3 ) ;
37+ const node4 = new ListNode ( 4 ) ;
38+ const node5 = new ListNode ( 5 ) ;
39+
40+ node1 . next = node2 ;
41+ node2 . next = node3 ;
42+ node3 . next = node4 ;
43+ node4 . next = node5 ;
44+
45+ // Print out linked list
46+ let current = node1 ;
47+ while ( current !== null ) {
48+ console . log ( current . val ) ;
49+ current = current . next ;
50+ }
51+
52+ // Reversed linked list
53+ const reversedHead = reverseList ( node1 ) ;
54+
55+ // Print out reversed linked list
56+ current = reversedHead ;
57+ while ( current !== null ) {
58+ console . log ( current . val ) ;
59+ current = current . next ;
60+ }
61+
62+ // TC: O(n)
63+ // SC: O(1)
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {boolean }
4+ */
5+ var isValid = function ( s ) {
6+ // Create stack array
7+ let stack = [ ] ;
8+ // Iterate string to find there is an open bracket
9+ for ( char of s ) {
10+ // If it is an open bracket, push the close bracket into stack array
11+ if ( char === "(" ) stack . push ( ")" ) ;
12+ else if ( char === "{" ) stack . push ( "}" ) ;
13+ else if ( char === "[" ) stack . push ( "]" ) ;
14+ // If it is not an open bracket, compare last element of stack array
15+ else if ( char !== stack . pop ( ) ) return false ;
16+ }
17+ // Check stack array still has any element after iteration
18+ return stack . length === 0 ;
19+ } ;
20+
21+ // TC: O(n)
22+ // MC: O(n)
23+
24+ console . log ( isValid ( "()" ) ) ; //true
25+ console . log ( isValid ( "()[]{}" ) ) ; //true
26+ console . log ( isValid ( "([)]" ) ) ; //false
You can’t perform that action at this time.
0 commit comments