Skip to content

Commit dbacbc9

Browse files
committed
feat: merge-two-sorted_lists 풀이
1 parent 91082d4 commit dbacbc9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

merge-two-sorted-lists/saysimple.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
if not (list1 and list2):
9+
return list1 if list1 else list2
10+
11+
ret = ListNode(0, None)
12+
head = ret
13+
14+
while list1 and list2:
15+
a, b = list1.val, list2.val
16+
print(a, b)
17+
18+
if a < b:
19+
print(1)
20+
next = ListNode(a, None)
21+
ret.next = next
22+
ret = next
23+
list1 = list1.next
24+
elif a > b:
25+
print(2)
26+
next = ListNode(b, None)
27+
ret.next = next
28+
ret = next
29+
list2 = list2.next
30+
else:
31+
for i in range(2):
32+
next = ListNode(a, None)
33+
ret.next = next
34+
ret = next
35+
list1, list2 = list1.next, list2.next
36+
print(head)
37+
38+
if list1:
39+
while list1:
40+
next = ListNode(list1.val, None)
41+
ret.next = next
42+
ret = next
43+
list1 = list1.next
44+
45+
if list2:
46+
while list2:
47+
next = ListNode(list2.val, None)
48+
ret.next = next
49+
ret = next
50+
list2 = list2.next
51+
52+
head = head.next
53+
54+
return head

0 commit comments

Comments
 (0)