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/ehlung.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
n = list1의 길이, m = list2의 길이
시간 복잡도: O(n + m) - 반복마다 노드 하나를 붙이고, 노드마다 한 번씩만 붙임
공간 복잡도: O(1) - 새로 만드는 노드는 시작용 노드 하나뿐이고, 기존 노드의 next만 바꿈
"""

# 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: ListNode | None, list2: ListNode | None) -> ListNode | None:
start = ListNode()
tail = start
while list1 is not None and list2 is not None:
if list1.val <= list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next
tail = tail.next
if list1 is not None:

@wlghsp wlghsp Dec 5, 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.

tail.next = list1 or list2 로 한줄로 바꿀 수 있겠네요. 수고하셨습니다.

tail.next = list1
else:
tail.next = list2
return start.next
Loading