File tree Expand file tree Collapse file tree 5 files changed +153
-0
lines changed Expand file tree Collapse file tree 5 files changed +153
-0
lines changed Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/invert-binary-tree/
2
+ - time complexity : O(n)
3
+ - space complexity : O(h), h = tree height
4
+ - https://algorithm.jonghoonpark.com/2024/03/31/leetcode-226
5
+
6
+ ``` java
7
+ public class Solution {
8
+ public TreeNode invertTree (TreeNode root ) {
9
+ if (root == null ) {
10
+ return null ;
11
+ }
12
+
13
+ var temp = root. left;
14
+ root. left = root. right;
15
+ root. right = temp;
16
+
17
+ if (root. left != null ) {
18
+ invertTree(root. left);
19
+ }
20
+ if (root. right != null ) {
21
+ invertTree(root. right);
22
+ }
23
+
24
+ return root;
25
+ }
26
+ }
27
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/linked-list-cycle
2
+ - time complexity : O(n)
3
+ - space complexity : O(1)
4
+ - https://algorithm.jonghoonpark.com/2024/02/15/leetcode-141
5
+
6
+ ``` java
7
+ public class Solution {
8
+ public boolean hasCycle (ListNode head ) {
9
+ if (head == null ) {
10
+ return false ;
11
+ }
12
+
13
+ ListNode p1 = head;
14
+ ListNode p2 = head;
15
+ while (p2 != null && p2. next != null ) {
16
+ p1 = p1. next;
17
+ p2 = p2. next. next;
18
+
19
+ if (p1 == p2) {
20
+ return true ;
21
+ }
22
+ }
23
+
24
+ return false ;
25
+ }
26
+ }
27
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/merge-two-sorted-lists/
2
+ - time complexity : O(n)
3
+ - space complexity : O(n)
4
+ - https://algorithm.jonghoonpark.com/2024/05/01/leetcode-21
5
+
6
+ ``` java
7
+ class Solution {
8
+ public ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
9
+ ListNode head = new ListNode (0 );
10
+ ListNode tail = head;
11
+
12
+ while (list1 != null && list2 != null ) {
13
+ if (list1. val < list2. val) {
14
+ tail. next = list1;
15
+ list1 = list1. next;
16
+ } else {
17
+ tail. next = list2;
18
+ list2 = list2. next;
19
+ }
20
+ tail = tail. next;
21
+ }
22
+
23
+ if (list1 != null ) {
24
+ tail. next = list1;
25
+ } else {
26
+ tail. next = list2;
27
+ }
28
+
29
+ return head. next;
30
+ }
31
+ }
32
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/reverse-linked-list/
2
+ - time complexity : O(n)
3
+ - space complexity : O(1)
4
+ - https://algorithm.jonghoonpark.com/2024/02/15/leetcode-206
5
+
6
+ ``` java
7
+ class Solution {
8
+ public ListNode reverseList (ListNode head ) {
9
+ if (head == null ) {
10
+ return null ;
11
+ }
12
+
13
+ ListNode p1 = head;
14
+ ListNode p2 = head. next;
15
+ p1. next = null ;
16
+
17
+ while (p2 != null ) {
18
+ ListNode temp = p2. next;
19
+ p2. next = p1;
20
+ p1 = p2;
21
+ p2 = temp;
22
+ }
23
+
24
+ return p1;
25
+ }
26
+ }
27
+ ```
Original file line number Diff line number Diff line change
1
+ - https://leetcode.com/problems/valid-parentheses/
2
+ - time complexity : O(n)
3
+ - space complexity : O(n)
4
+ - https://algorithm.jonghoonpark.com/2024/04/29/leetcode-20
5
+
6
+ ``` java
7
+ class Solution {
8
+ public boolean isValid (String s ) {
9
+ Stack<Character > stack = new Stack<> ();
10
+
11
+ for (char c : s. toCharArray()) {
12
+ if (c == ' (' || c == ' {' || c == ' [' ) {
13
+ stack. push(c);
14
+ continue ;
15
+ }
16
+
17
+ if (stack. isEmpty()) {
18
+ return false ;
19
+ }
20
+
21
+ boolean valid = false ;
22
+ if (c == ' )' ) {
23
+ valid = stack. peek() == ' (' ;
24
+ } else if (c == ' }' ) {
25
+ valid = stack. peek() == ' {' ;
26
+ } else if (c == ' ]' ) {
27
+ valid = stack. peek() == ' [' ;
28
+ }
29
+
30
+ if (valid) {
31
+ stack. pop();
32
+ } else {
33
+ return false ;
34
+ }
35
+ }
36
+
37
+ return stack. isEmpty();
38
+ }
39
+ }
40
+ ```
You canโt perform that action at this time.
0 commit comments