Skip to content

Commit 85c5b26

Browse files
committed
add: #224 Merge Two Sorted Lists 두번째 풀이 추가
1 parent 2830df0 commit 85c5b26

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

merge-two-sorted-lists/sukyoungshin.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ function mergeTwoLists(
3737
current.next = list1 !== null ? list1 : list2;
3838
return dummy.next;
3939
};
40+
41+
// 2번째 풀이 (재귀)
42+
function mergeTwoLists2(
43+
list1: ListNode | null,
44+
list2: ListNode | null
45+
): ListNode | null {
46+
if (!(list1 && list2)) return list1 || list2;
47+
if (list1.val < list2.val) {
48+
list1.next = mergeTwoLists(list1.next, list2);
49+
return list1;
50+
} else {
51+
list2.next = mergeTwoLists(list1, list2.next);
52+
return list2;
53+
}
54+
};

0 commit comments

Comments
 (0)