Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions merge-two-sorted-lists/togo26.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Linked List
  • 설명: 두 개의 정렬된 리스트를 하나의 정렬된 리스트로 합치는 문제로, 두 포인터를 번갈아가며 각 리스트를 순회하며 노드를 이어 붙인다. 공간복잡도 O(1)로 목적으로 두 포인터와 기존 노드를 재사용하는 패턴이다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n + m) O(n + m)
Space O(n + m) O(1)

피드백: 두 포인터와 인라인 노드 재사용으로 추가 공간 없이 합치도록 구현했다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/

// TC: O(n + m) / SC: O(1)
var mergeTwoLists = function (list1, list2) {
let mergeHead = null;
let mergePointer = null;

let head1 = list1;
let head2 = list2;

const mergeNode = node => {
const temp = node.next;
if (!mergeHead) {
mergeHead = node;
mergePointer = node;
} else {
mergePointer.next = node;
mergePointer = mergePointer.next;
}
return temp;
};

while (head1 && head2) {
if (head1.val <= head2.val) {
head1 = mergeNode(head1);
} else {
head2 = mergeNode(head2);
}
}

if (head1) mergeNode(head1);
if (head2) mergeNode(head2);

return mergeHead;
Comment on lines +15 to +45

@parkhojeong parkhojeong Jul 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy 류의 노드를 두면 훨씬 간결하게 풀 수 있어서 이렇게도 풀어보시는걸 추천드립니다.
다른 방법으로는 재귀로 풀면 아주 직관적으로도 풀 수 있어서 이 방법도 풀어보시면 좋을 거 같습니다.

};
Loading