Skip to content

Commit e352782

Browse files
authored
Merge pull request #2300 from smosco/main
[smosco] WEEK 12 solutions
2 parents 1c0bddb + 33bca1f commit e352782

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

reorder-list/smosco.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
9+
/**
10+
* 방법 1: 반으로 나누기 + 뒤집기 + 합치기
11+
* 시간 복잡도: O(n)
12+
* 공간 복잡도: O(1)
13+
* @param {ListNode} head
14+
* @return {void} Do not return anything, modify head in-place instead.
15+
*/
16+
var reorderList = function (head) {
17+
if (!head || !head.next) return;
18+
19+
// 1. 중간 지점 찾기 (slow-fast pointer)
20+
let slow = head,
21+
fast = head;
22+
while (fast.next && fast.next.next) {
23+
slow = slow.next;
24+
fast = fast.next.next;
25+
}
26+
27+
// 2. 뒷부분 리스트 분리 및 뒤집기
28+
let second = slow.next;
29+
slow.next = null;
30+
31+
let prev = null;
32+
while (second) {
33+
let temp = second.next;
34+
second.next = prev;
35+
prev = second;
36+
second = temp;
37+
}
38+
39+
// 3. 두 리스트 합치기
40+
let first = head;
41+
second = prev;
42+
while (second) {
43+
let temp1 = first.next;
44+
let temp2 = second.next;
45+
first.next = second;
46+
second.next = temp1;
47+
first = temp1;
48+
second = temp2;
49+
}
50+
};
51+
52+
/**
53+
* 방법 2: Stack 사용
54+
* 시간 복잡도: O(n)
55+
* 공간 복잡도: O(n)
56+
* @param {ListNode} head
57+
* @return {void} Do not return anything, modify head in-place instead.
58+
*/
59+
var reorderListStack = function (head) {
60+
if (!head || !head.next) return;
61+
62+
// 모든 노드를 스택에 저장
63+
let stack = [];
64+
let curr = head;
65+
while (curr) {
66+
stack.push(curr);
67+
curr = curr.next;
68+
}
69+
70+
// 앞에서부터, 뒤에서부터 번갈아 연결
71+
let len = stack.length;
72+
curr = head;
73+
for (let i = 0; i < Math.floor(len / 2); i++) {
74+
let last = stack.pop();
75+
let temp = curr.next;
76+
curr.next = last;
77+
last.next = temp;
78+
curr = temp;
79+
}
80+
curr.next = null; // 마지막 노드 처리
81+
};

same-tree/smosco.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function(p, q) {
15+
// 두 노드가 모두 null이면 같은 트리
16+
if (p === null && q === null) {
17+
return true;
18+
}
19+
20+
// 하나만 null이면 다른 트리
21+
if (p === null || q === null) {
22+
return false;
23+
}
24+
25+
// 값이 다르면 다른 트리
26+
if (p.val !== q.val) {
27+
return false;
28+
}
29+
30+
// 왼쪽과 오른쪽 서브트리를 재귀적으로 비교
31+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
32+
};

0 commit comments

Comments
 (0)