Skip to content
Merged
Changes from 3 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
47 changes: 47 additions & 0 deletions merge-two-sorted-lists/river20s.py
Copy link
Contributor

Choose a reason for hiding this comment

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

시간복잡도 O(m+n) 풀이한 코드를 보며 저도 많은 도움이 되었습니다.
이번주차도 고생하셨습니다~!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

승인 감사합니다! :-)

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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]
- TC: O(m+n)
- SC: O(1)
"""

# 엣지 케이스
# 두 리스트 중 하나라도 비어 있는 경우,
# 나머지 리스트 바로 반환
if not list1:
return list2
if not list2:
return list1

# 더미 헤드 노드
# 결과 리스트 시작점 역할을 할 가짜 노드 생성
dummy = ListNode()
current = dummy # current는 현재까지 만든 리스트의 마지막 노드

# 두 리스트 모두 노드가 남아 있을 때까지 반복
while list1 and list2:
if list1.val <= list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next

# 남은 노드들 이어 붙이기
# 아직 노드가 남아 있는 리스트가 있으면 통째로 붙이기
if list1:
current.next = list1
elif list2:
current.next = list2

return dummy.next