题目

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Python

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
class Solution:
def mergeTwoLists(self, l1, l2):
a, b = l1, l2
h = None
t = h
while a is not None and b is not None:
z = None
if a.val <= b.val:
z = a
a = a.next
else:
z = b
b = b.next
if t is None:
h = z
t = h
else:
t.next = z
t = t.next
z = a if a is not None else b
if z is not None:
if t is None:
h = z
else:
t.next = z
return h