-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.py
More file actions
32 lines (32 loc) · 913 Bytes
/
21.py
File metadata and controls
32 lines (32 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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]
"""
l1=list1
l2=list2
dummy_head=ListNode(0)
dummy_head.next=l1
pre_l1=dummy_head
while l1 is not None and l2 is not None:
if l2.val<l1.val:
temp2=l2.next
l2.next=l1
pre_l1.next=l2
l2=temp2
pre_l1=pre_l1.next
else:
l1=l1.next
pre_l1=pre_l1.next
if l1 is not None:
pass
if l2 is not None:
pre_l1.next=l2
return dummy_head.next