Skip to content
Open
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
28 changes: 28 additions & 0 deletions merge-two-sorted-lists/chapse57.py

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, Merge sort pattern?
  • 설명: 두 포인터(list1, list2)로 연결 리스트를 순차적으로 비교해 합치는 방식으로, 빈 리스트까지 순회하며 새로운 연결 리스트를 구성한다. 동시에 포인터를 한 칸씩 전진시키는 Two Pointers 패턴과 연결 리스트의 합치는 상황에 해당한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n + m)
Space O(1)

피드백: 두 리스트를 순차적으로 비교하며 새 노드 없이 기존 노드를 재사용해 병합한다. 추가 포인터/더미 노드를 사용해 구현이 명확하다.

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

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
dummy = ListNode()
tail = dummy


while list1 and list2:
if list1.val <= list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next

tail = tail.next
tail.next = list1 if list1 else list2

return dummy.next

@parkhojeong parkhojeong Jul 17, 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 가 무슨 값을 가지는지 생각을 해야했던 거 같습니다. dummy_head 같은 약간은 의미를 드러내는 것이면 어떨까 생각해봤습니다.

Loading