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.
Subscribe to see which companies asked this question
没啥可说的,一个一个比较,然后放到res里去
class Solution(object):
def mergeTwoLists(self, l1, l2):
res = ListNode(0)
p = res
while l1 and l2:
if l1.val>l2.val:
p.next = l2
l2 = l2.next
p = p.next
else:
p.next = l1
l1 = l1.next
p = p.next
if l1:
p.next = l1
if l2:
p.next = l2
return res.next