diff --git a/invert-binary-tree/dev-jonghoonpark.md b/invert-binary-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..c30b03d07 --- /dev/null +++ b/invert-binary-tree/dev-jonghoonpark.md @@ -0,0 +1,27 @@ +- https://leetcode.com/problems/invert-binary-tree/ +- time complexity : O(n) +- space complexity : O(h), h = tree height +- https://algorithm.jonghoonpark.com/2024/03/31/leetcode-226 + +```java +public class Solution { + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return null; + } + + var temp = root.left; + root.left = root.right; + root.right = temp; + + if (root.left != null) { + invertTree(root.left); + } + if (root.right != null) { + invertTree(root.right); + } + + return root; + } +} +``` diff --git a/linked-list-cycle/dev-jonghoonpark.md b/linked-list-cycle/dev-jonghoonpark.md new file mode 100644 index 000000000..cf7cea277 --- /dev/null +++ b/linked-list-cycle/dev-jonghoonpark.md @@ -0,0 +1,27 @@ +- https://leetcode.com/problems/linked-list-cycle +- time complexity : O(n) +- space complexity : O(1) +- https://algorithm.jonghoonpark.com/2024/02/15/leetcode-141 + +```java +public class Solution { + public boolean hasCycle(ListNode head) { + if (head == null) { + return false; + } + + ListNode p1 = head; + ListNode p2 = head; + while(p2 != null && p2.next != null) { + p1 = p1.next; + p2 = p2.next.next; + + if (p1 == p2) { + return true; + } + } + + return false; + } +} +``` diff --git a/merge-two-sorted-lists/dev-jonghoonpark.md b/merge-two-sorted-lists/dev-jonghoonpark.md new file mode 100644 index 000000000..e8d5de20a --- /dev/null +++ b/merge-two-sorted-lists/dev-jonghoonpark.md @@ -0,0 +1,32 @@ +- https://leetcode.com/problems/merge-two-sorted-lists/ +- time complexity : O(n) +- space complexity : O(n) +- https://algorithm.jonghoonpark.com/2024/05/01/leetcode-21 + +```java +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode head = new ListNode(0); + ListNode tail = head; + + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + tail.next = list1; + list1 = list1.next; + } else { + tail.next = list2; + list2 = list2.next; + } + tail = tail.next; + } + + if (list1 != null) { + tail.next = list1; + } else { + tail.next = list2; + } + + return head.next; + } +} +``` diff --git a/reverse-linked-list/dev-jonghoonpark.md b/reverse-linked-list/dev-jonghoonpark.md new file mode 100644 index 000000000..7b7350fd2 --- /dev/null +++ b/reverse-linked-list/dev-jonghoonpark.md @@ -0,0 +1,27 @@ +- https://leetcode.com/problems/reverse-linked-list/ +- time complexity : O(n) +- space complexity : O(1) +- https://algorithm.jonghoonpark.com/2024/02/15/leetcode-206 + +```java +class Solution { + public ListNode reverseList(ListNode head) { + if(head == null) { + return null; + } + + ListNode p1 = head; + ListNode p2 = head.next; + p1.next = null; + + while (p2 != null) { + ListNode temp = p2.next; + p2.next = p1; + p1 = p2; + p2 = temp; + } + + return p1; + } +} +``` diff --git a/valid-parentheses/dev-jonghoonpark.md b/valid-parentheses/dev-jonghoonpark.md new file mode 100644 index 000000000..e866ca0e4 --- /dev/null +++ b/valid-parentheses/dev-jonghoonpark.md @@ -0,0 +1,40 @@ +- https://leetcode.com/problems/valid-parentheses/ +- time complexity : O(n) +- space complexity : O(n) +- https://algorithm.jonghoonpark.com/2024/04/29/leetcode-20 + +```java +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + + for (char c : s.toCharArray()) { + if (c == '(' || c == '{' || c == '[') { + stack.push(c); + continue; + } + + if (stack.isEmpty()) { + return false; + } + + boolean valid = false; + if (c == ')') { + valid = stack.peek() == '('; + } else if (c == '}') { + valid = stack.peek() == '{'; + } else if (c == ']') { + valid = stack.peek() == '['; + } + + if (valid) { + stack.pop(); + } else { + return false; + } + } + + return stack.isEmpty(); + } +} +```