Skip to content

Commit e0acb77

Browse files
authored
Merge pull request #509 from JEONGHWANMIN/main
[환미니니] 8주차 문제풀이 제출
2 parents ad69c40 + a4473c8 commit e0acb77

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

clone-graph/hwanmini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 노드 수 v, 간선 e
2+
// 시간복잡도 O(v+e)
3+
// 공간복잡도 O(v+e)
4+
5+
/**
6+
* // Definition for a _Node.
7+
* function _Node(val, neighbors) {
8+
* this.val = val === undefined ? 0 : val;
9+
* this.neighbors = neighbors === undefined ? [] : neighbors;
10+
* };
11+
*/
12+
13+
/**
14+
* @param {_Node} node
15+
* @return {_Node}
16+
*/
17+
var cloneGraph = function(node) {
18+
if (!node) return
19+
20+
const visited = new Map()
21+
visited.set(node, new _Node(node.val))
22+
23+
const que = [node]
24+
25+
while (que.length) {
26+
const curNode = que.shift()
27+
28+
for (neighbor of curNode.neighbors) {
29+
if (!visited.has(neighbor)) {
30+
visited.set(neighbor, new _Node(neighbor.val));
31+
que.push(neighbor)
32+
}
33+
34+
visited.get(curNode).neighbors.push(visited.get(neighbor))
35+
}
36+
}
37+
38+
return visited.get(node)
39+
};

merge-two-sorted-lists/hwanmini.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// m: list1, n: list2
2+
// 시간복잡도: O(m + n)
3+
// 공간복잡도: O(m + n)
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* function ListNode(val, next) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.next = (next===undefined ? null : next)
10+
* }
11+
*/
12+
/**
13+
* @param {ListNode} list1
14+
* @param {ListNode} list2
15+
* @return {ListNode}
16+
*/
17+
var mergeTwoLists = function(list1, list2) {
18+
let res = new ListNode()
19+
let resCopy = res
20+
21+
while (list1 && list2) {
22+
if (list1.val < list2.val) {
23+
res.next = list1
24+
list1 = list1.next;
25+
} else {
26+
res.next = list2
27+
list2 = list2.next;
28+
}
29+
30+
res = res.next
31+
}
32+
33+
if (list1) res.next = list1;
34+
if (list2) res.next = list2
35+
36+
return resCopy.next
37+
};
38+

0 commit comments

Comments
 (0)