Skip to content
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
24 changes: 24 additions & 0 deletions merge-two-sorted-lists/freemjstudio.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 Type (not in list), Linked List
  • 설명: 두 개의 정렬된 연결 리스트를 순회하며 포인터 두 개로 작은 값을 차례로 연결 리스트에 붙이는 전형적인 Two Pointers 패턴이며, 연결 리스트를 다루는 방식이 포함되어 있습니다.

📊 시간/공간 복잡도 분석

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

피드백: 두 포인터를 이용해 차례대로 노드를 연결 리스트에 붙이고 남은 노드를 뒤에 연결한다.

개선 제안: 재귀 방식으로도 구현 가능하나 스택 깊이가 커질 수 있어 현재의 반복 구현이 안정적이다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
merged_list = dummy
Comment thread
freemjstudio marked this conversation as resolved.

while list1 is not None and list2 is not None:
if list1.val <= list2.val:
merged_list.next = list1
list1 = list1.next
else:
merged_list.next = list2
list2 = list2.next
# merged_list 포인터를 한칸 이동시켜서 다음 노드 붙일 준비
merged_list = merged_list.next

# 남아 있는 노드 붙이기
merged_list.next = list1 if list1 is not None else list2
Comment thread
freemjstudio marked this conversation as resolved.

return dummy.next