๐
ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ ์ธํฐ๋ทฐ
์ฑ ์ ์ฐธ๊ณ ํ์ต๋๋ค.
๋ ๊ฐ์ ์ ๋ ฌ ๋ ์ฐ๊ฒฐ ๋ชฉ๋ก์ ๋ณํฉํ๊ณ ์ ๋ ฌ ๋ ๋ชฉ๋ก์ผ๋ก ๋ฐํํฉ๋๋ค. ๋ชฉ๋ก์ ์ฒ์ ๋ ๋ชฉ๋ก์ ๋ ธ๋๋ฅผ ์ฐ๊ฒฐํ์ฌ ๋ง๋ค์ด์ผํฉ๋๋ค.
์ฌ๊ท์ ์ผ๋ก ํธ๋ ๋ฐฉ๋ฒ๊ณผ iterativeํ๊ฒ ํธ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
def mergeTwoLists(self, l1, l2):
dummy = cur = ListNode(0)
while l1 and l2:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 or l2
return dummy.next
def mergeTwoListsRecursive(self, l1, l2):
if not l1 or not l2:
return l1 or l2
if l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2