File tree Expand file tree Collapse file tree 5 files changed +125
-0
lines changed Expand file tree Collapse file tree 5 files changed +125
-0
lines changed Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
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)
You can’t perform that action at this time.
0 commit comments