Skip to content

Commit 9f72819

Browse files
committed
solve: mergeTwoSortedLists
1 parent 46aac57 commit 9f72819

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

merge-two-sorted-lists/b41-41.ts

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
14+
15+
/**
16+
* 방법1: [실패]
17+
* - 접근법: for문 사용, list1, list2 중 긴 값을 기준으로 for문 최대 값 설정
18+
* - 실패 이유: LinkedList를 배열처럼 접근하려 했으나, LinkedList는 .next로 순차 접근해야 함
19+
* 또한 list1.next.length와 같은 속성이 존재하지 않음
20+
*/
21+
// const getSortedList1 = () => {
22+
// const maxLength = Math.max(list1.next.length, list2.next.length) + 1; // 오류: LinkedList에는 length 속성이 없음
23+
// const results = [];
24+
// const queue = [];
25+
26+
// for(let i = 0; i < maxLength; i++) {
27+
// if(i === 0) {
28+
// if(list1.val < list2.val) {
29+
// results.push(list1.val);
30+
// results.push(list2.val);
31+
// } else {
32+
// results.push(list2.val);
33+
// results.push(list1.val);
34+
// }
35+
// } else {
36+
// const currList1Val = list1.next[i - 1]; // 오류: LinkedList는 인덱스로 접근 불가
37+
// const currList2Val = list2.next[i - 2]; // 오류: LinkedList는 인덱스로 접근 불가
38+
// const resultsLatestVal = results[reulsts.length - 1]; // 오류: 변수명 오타 (reulsts)
39+
// if(currList1Val < currList2Val) {
40+
// if(currList1Val < resultsLatestVal) {
41+
42+
// }
43+
// }
44+
// }
45+
// }
46+
// };
47+
48+
/**
49+
* 방법2: [실패]
50+
* - 접근법: LinkedList를 배열로 변환 후 정렬
51+
* - 실패 이유: LinkedList를 배열로 변환하는 과정에서 오류 발생
52+
* list1.next와 list2.next는 배열이 아닌 ListNode 객체이므로 스프레드 연산자 사용 불가
53+
* 또한 결과를 배열로 반환하지만 문제는 ListNode를 요구함
54+
*/
55+
// const getSortedList2 = () => {
56+
// const list1Next = list1.next ?? [];
57+
// const list2Next = list2.next ?? [];
58+
// console.log('debug1::', list1.val, list2.val, list1Next, list2Next)
59+
// const result: number[] = [list1.val, list2.val, ...list1Next, ...list2Next]; // 오류: ListNode 객체는 스프레드 연산자로 펼칠 수 없음
60+
61+
// console.log('debug::', result)
62+
63+
// return list1; // 오류: 정렬된 결과가 아닌 원본 list1을 반환
64+
// };
65+
66+
/**
67+
* 방법3: [실패]
68+
* - 접근법: for 문으로 next 없을 때까지 체크해서 배열에 추가한 다음 정렬 후 반환
69+
* - 실패 이유: 배열에 값을 모으기만 하고 정렬 및 ListNode 생성 로직이 누락됨
70+
* 또한 for 루프 조건이 잘못됨 (!!isEnd는 항상 false로 루프가 실행되지 않음)
71+
* 마지막에 return 문이 불완전함
72+
*/
73+
// const getSortedList3 = () => {
74+
// const result = [list1.val, list2.val];
75+
// let currList1 = list1.next;
76+
// let currList2 = list2.next;
77+
78+
// let isEnd = false;
79+
// for (let i = 0; !!isEnd; i++) { // 오류: !!isEnd는 항상 false (isEnd가 false로 초기화됨)
80+
// if(currList1?.val) {
81+
// result.push(currList1.val);
82+
// }
83+
// if(currList2?.val) {
84+
// result.push(currList2.val);
85+
// }
86+
87+
// if(currList1?.next) {
88+
// currList1 = currList1.next;
89+
// } else if (currList2?.next) {
90+
// currList2 = currList2.next;
91+
// } else {
92+
// break;
93+
// }
94+
// }
95+
96+
// return // 오류: 반환값이 없음
97+
// };
98+
99+
/**
100+
* 방법4: [실패]
101+
* - 접근법: ListNode 인스턴스로 결과 생성, head를 만들고 순차적으로 값 추가
102+
* - 실패 이유: 구현이 완료되지 않음, 아이디어만 주석으로 남겨둠
103+
*/
104+
105+
/**
106+
* 방법5:
107+
* - 접근법: 새 ListNode 인스턴스를 생성하여 두 리스트의 값을 순차적으로 비교하며 병합
108+
* - 시간 복잡도: O(n + m)
109+
* - 공간 복잡도: O(n + m)
110+
*/
111+
const getSortedList5 = () => {
112+
const dummy = new ListNode();
113+
let current = dummy;
114+
115+
while (list1 && list2) {
116+
if (list1.val < list2.val) {
117+
current.next = new ListNode(list1.val);
118+
list1 = list1.next;
119+
} else {
120+
current.next = new ListNode(list2.val);
121+
list2 = list2.next;
122+
}
123+
current = current.next;
124+
}
125+
126+
while (!!list1) {
127+
current.next = new ListNode(list1.val);
128+
list1 = list1?.next;
129+
current = current.next
130+
}
131+
132+
while (!!list2) {
133+
current.next = new ListNode(list2.val);
134+
list2 = list2?.next;
135+
current = current.next
136+
}
137+
138+
return dummy.next;
139+
};
140+
141+
/**
142+
* 방법6: with GPT
143+
* - 접근법: 방법5의 최적화 버전으로, 새 노드를 생성하지 않고 기존 노드를 재사용
144+
* - 시간 복잡도: O(n + m)
145+
* - 공간 복잡도: O(1)
146+
*/
147+
const getSortedList6 = () => {
148+
const dummy = new ListNode();
149+
let current = dummy;
150+
151+
while (list1 && list2) {
152+
if (list1.val < list2.val) {
153+
current.next = list1;
154+
list1 = list1.next;
155+
} else {
156+
current.next = list2;
157+
list2 = list2.next;
158+
}
159+
current = current.next;
160+
}
161+
162+
current.next = list1 ?? list2;
163+
164+
return dummy.next;
165+
};
166+
167+
// return getSortedList1();
168+
// return getSortedList2();
169+
// return getSortedList3();
170+
// return getSortedList5();
171+
return getSortedList6();
172+
173+
};

0 commit comments

Comments
 (0)