-
-
Notifications
You must be signed in to change notification settings - Fork 305
[smosco] WEEK 12 solutions #2300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val, next) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| */ | ||
|
|
||
| /** | ||
| * 방법 1: 반으로 나누기 + 뒤집기 + 합치기 | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(1) | ||
| * @param {ListNode} head | ||
| * @return {void} Do not return anything, modify head in-place instead. | ||
| */ | ||
| var reorderList = function (head) { | ||
| if (!head || !head.next) return; | ||
|
|
||
| // 1. 중간 지점 찾기 (slow-fast pointer) | ||
| let slow = head, | ||
| fast = head; | ||
| while (fast.next && fast.next.next) { | ||
| slow = slow.next; | ||
| fast = fast.next.next; | ||
| } | ||
|
|
||
| // 2. 뒷부분 리스트 분리 및 뒤집기 | ||
| let second = slow.next; | ||
| slow.next = null; | ||
|
|
||
| let prev = null; | ||
| while (second) { | ||
| let temp = second.next; | ||
| second.next = prev; | ||
| prev = second; | ||
| second = temp; | ||
| } | ||
|
|
||
| // 3. 두 리스트 합치기 | ||
| let first = head; | ||
| second = prev; | ||
| while (second) { | ||
| let temp1 = first.next; | ||
| let temp2 = second.next; | ||
| first.next = second; | ||
| second.next = temp1; | ||
| first = temp1; | ||
| second = temp2; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 방법 2: Stack 사용 | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| * @param {ListNode} head | ||
| * @return {void} Do not return anything, modify head in-place instead. | ||
| */ | ||
| var reorderListStack = function (head) { | ||
| if (!head || !head.next) return; | ||
|
|
||
| // 모든 노드를 스택에 저장 | ||
| let stack = []; | ||
| let curr = head; | ||
| while (curr) { | ||
| stack.push(curr); | ||
| curr = curr.next; | ||
| } | ||
|
|
||
| // 앞에서부터, 뒤에서부터 번갈아 연결 | ||
| let len = stack.length; | ||
| curr = head; | ||
| for (let i = 0; i < Math.floor(len / 2); i++) { | ||
| let last = stack.pop(); | ||
| let temp = curr.next; | ||
| curr.next = last; | ||
| last.next = temp; | ||
| curr = temp; | ||
| } | ||
| curr.next = null; // 마지막 노드 처리 | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} p | ||
| * @param {TreeNode} q | ||
| * @return {boolean} | ||
| */ | ||
| var isSameTree = function(p, q) { | ||
| // 두 노드가 모두 null이면 같은 트리 | ||
| if (p === null && q === null) { | ||
| return true; | ||
| } | ||
|
|
||
| // 하나만 null이면 다른 트리 | ||
| if (p === null || q === null) { | ||
| return false; | ||
| } | ||
|
|
||
| // 값이 다르면 다른 트리 | ||
| if (p.val !== q.val) { | ||
| return false; | ||
| } | ||
|
|
||
| // 왼쪽과 오른쪽 서브트리를 재귀적으로 비교 | ||
| return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저와 정확히 같은 방식을 사용하셔서 동질감이 들었습니다ㅋㅋ