-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_tsl.py
More file actions
33 lines (26 loc) · 786 Bytes
/
merge_tsl.py
File metadata and controls
33 lines (26 loc) · 786 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
33
# 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, list2):
if list1 and not list2:
return list1
if list2 and not list1:
return list2
if not list1 and not list2:
return None
dummy = ListNode()
curr = dummy
while list1 and list2:
if list1.val < list2.val:
curr.next = list1
curr = list1
list1 = list1.next
else:
curr.next = list2
curr = list2
list2 = list2.next
curr.next = list1 if list1 else list2
return dummy.next