Skip to content

Commit 5ba2a46

Browse files
week2 mission done
1 parent d563667 commit 5ba2a46

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- https://leetcode.com/problems/invert-binary-tree/
2+
- time complexity : O(n)
3+
- space complexity : O(log n)
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+
```

linked-list-cycle/dev-jonghoonpark.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 = null;
10+
ListNode tail = null;
11+
12+
while(!(list1 == null && list2 == null)) {
13+
ListNode selected;
14+
if (list1 == null) {
15+
selected = list2;
16+
list2 = list2.next;
17+
} else if (list2 == null) {
18+
selected = list1;
19+
list1 = list1.next;
20+
} else if (list1.val < list2.val) {
21+
selected = list1;
22+
list1 = list1.next;
23+
} else {
24+
selected = list2;
25+
list2 = list2.next;
26+
}
27+
28+
ListNode newNode = new ListNode(selected.val);
29+
if(head == null) {
30+
head = newNode;
31+
} else {
32+
tail.next = newNode;
33+
}
34+
35+
tail = newNode;
36+
}
37+
38+
return head;
39+
}
40+
}
41+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
```

valid-parentheses/dev-jonghoonpark.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
```

0 commit comments

Comments
 (0)