This commit is contained in:
Gyubin Han
2025-02-19 14:09:55 +09:00
parent e3c6c8b32a
commit dede0dca01
36 changed files with 555 additions and 0 deletions

37
leetcode/21.py Normal file
View File

@@ -0,0 +1,37 @@
# 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]:
result=ListNode()
re=result
li1=list1
li2=list2
while not li1==None and not li2==None:
n=ListNode()
if li1.val<li2.val:
n.val=li1.val
li1=li1.next
else:
n.val=li2.val
li2=li2.next
re.next=n
re=re.next
while not li1==None:
n=ListNode()
n.val=li1.val
re.next=n
re=re.next
li1=li1.next
while not li2==None:
n=ListNode()
n.val=li2.val
re.next=n
re=re.next
li2=li2.next
return result.next