Skip to content

[std-freejia] week04 solutions #1822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2025
Merged
Changes from 1 commit
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/std-freejia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 고생한 이유: ListNode 의 시작점을 따로 둬야 하는 것!
*
* ListNode dummy = new ListNode(0); // 시작점
* ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터
*
*
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
// 길이가 둘다 null 이면, return null
if (list1 == null && list2 == null) return null;
Comment on lines +19 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

생략해도 상관없지만 명시해 주신 것 같네요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요 리뷰어님 코멘트 감사합니다!


ListNode dummy = new ListNode(0); // 시작점
ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ListNode dummy = new ListNode(0); // 시작점
ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터
ListNode dummy = new ListNode(); // 시작점

둘다 똑같은 시작점이므로 // 시작점 뒤에 실제 작업용 포인터라는 설명은 틀린 것 같아요.
한 줄로 합쳐도 되겠는데 명시해 주신 것 같네요.
또한 0을 안 넘겨도 무방하겠군요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dummy 는 고정된 시작점이고, output 은 이어붙일 포인터라서 따로 관리했습니다.
한 줄로 합치려고 했는데, accept 되지 못했습니다

Copy link
Contributor

Choose a reason for hiding this comment

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

오, 맞네요! 제가 잘못 생각했습니다.
output은 Iterator의 역할이군요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

제 코드에서는 더 나은 네이밍이 필요한 것 같네요. 리뷰 감사합니다!


while(list1 != null && list2 != null) {

if (list1.val < list2.val) {
output.next = list1;
list1 = list1.next; // list1 포인터 이동
} else {
output.next = list2;
list2 = list2.next; // list2 포인터 이동
}
output = output.next; // output 포인터 이동
}

if (list1 != null) {
output.next = list1;
}
if (list2 != null) {
output.next = list2;
}

return dummy.next;
}
}