From 5ba2a463e17f76df7a7fbb0acbc5af75ec3ade87 Mon Sep 17 00:00:00 2001 From: jonghoonpark Date: Wed, 1 May 2024 14:56:44 +0900 Subject: [PATCH 1/2] week2 mission done --- invert-binary-tree/dev-jonghoonpark.md | 27 ++++++++++++++ linked-list-cycle/dev-jonghoonpark.md | 27 ++++++++++++++ merge-two-sorted-lists/dev-jonghoonpark.md | 41 ++++++++++++++++++++++ reverse-linked-list/dev-jonghoonpark.md | 27 ++++++++++++++ valid-parentheses/dev-jonghoonpark.md | 40 +++++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 invert-binary-tree/dev-jonghoonpark.md create mode 100644 linked-list-cycle/dev-jonghoonpark.md create mode 100644 merge-two-sorted-lists/dev-jonghoonpark.md create mode 100644 reverse-linked-list/dev-jonghoonpark.md create mode 100644 valid-parentheses/dev-jonghoonpark.md diff --git a/invert-binary-tree/dev-jonghoonpark.md b/invert-binary-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..874c4d15f --- /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(log n) +- 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..99d0943f8 --- /dev/null +++ b/merge-two-sorted-lists/dev-jonghoonpark.md @@ -0,0 +1,41 @@ +- 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 = null; + ListNode tail = null; + + while(!(list1 == null && list2 == null)) { + ListNode selected; + if (list1 == null) { + selected = list2; + list2 = list2.next; + } else if (list2 == null) { + selected = list1; + list1 = list1.next; + } else if (list1.val < list2.val) { + selected = list1; + list1 = list1.next; + } else { + selected = list2; + list2 = list2.next; + } + + ListNode newNode = new ListNode(selected.val); + if(head == null) { + head = newNode; + } else { + tail.next = newNode; + } + + tail = newNode; + } + + return head; + } +} +``` 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(); + } +} +``` From 4e699c52d029e819326e0f1ac3fe4f1907034629 Mon Sep 17 00:00:00 2001 From: jonghoonpark Date: Wed, 8 May 2024 20:49:44 +0900 Subject: [PATCH 2/2] apply feedback --- invert-binary-tree/dev-jonghoonpark.md | 2 +- merge-two-sorted-lists/dev-jonghoonpark.md | 35 ++++++++-------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/invert-binary-tree/dev-jonghoonpark.md b/invert-binary-tree/dev-jonghoonpark.md index 874c4d15f..c30b03d07 100644 --- a/invert-binary-tree/dev-jonghoonpark.md +++ b/invert-binary-tree/dev-jonghoonpark.md @@ -1,6 +1,6 @@ - https://leetcode.com/problems/invert-binary-tree/ - time complexity : O(n) -- space complexity : O(log n) +- space complexity : O(h), h = tree height - https://algorithm.jonghoonpark.com/2024/03/31/leetcode-226 ```java diff --git a/merge-two-sorted-lists/dev-jonghoonpark.md b/merge-two-sorted-lists/dev-jonghoonpark.md index 99d0943f8..e8d5de20a 100644 --- a/merge-two-sorted-lists/dev-jonghoonpark.md +++ b/merge-two-sorted-lists/dev-jonghoonpark.md @@ -6,36 +6,27 @@ ```java class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { - ListNode head = null; - ListNode tail = null; + ListNode head = new ListNode(0); + ListNode tail = head; - while(!(list1 == null && list2 == null)) { - ListNode selected; - if (list1 == null) { - selected = list2; - list2 = list2.next; - } else if (list2 == null) { - selected = list1; - list1 = list1.next; - } else if (list1.val < list2.val) { - selected = list1; + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + tail.next = list1; list1 = list1.next; } else { - selected = list2; + tail.next = list2; list2 = list2.next; } + tail = tail.next; + } - ListNode newNode = new ListNode(selected.val); - if(head == null) { - head = newNode; - } else { - tail.next = newNode; - } - - tail = newNode; + if (list1 != null) { + tail.next = list1; + } else { + tail.next = list2; } - return head; + return head.next; } } ```