Skip to content

Commit 755bda4

Browse files
author
sangbeenmoon
committed
solved merge-two-sorted-lists.
1 parent 872ccec commit 755bda4

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
9+
cur1 = list1
10+
cur2 = list2
11+
12+
answer = ListNode()
13+
head = answer
14+
15+
16+
while cur1 != None:
17+
if cur2 != None:
18+
if cur1.val >= cur2.val:
19+
answer.next = ListNode(cur2.val)
20+
cur2 = cur2.next
21+
answer = answer.next
22+
else:
23+
answer.next = ListNode(cur1.val)
24+
cur1 = cur1.next
25+
answer = answer.next
26+
else:
27+
answer.next = ListNode(cur1.val)
28+
cur1 = cur1.next
29+
answer = answer.next
30+
31+
while cur2 != None:
32+
answer.next = ListNode(cur2.val)
33+
cur2 = cur2.next
34+
answer = answer.next
35+
return head.next

0 commit comments

Comments
 (0)