- 不是很难,没得优化,所以自行优化
自己:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
head_res = ListNode(-1)
p = head_res
l = l1
r = l2
while True:
print(l,r)
if l and r:
if l.val <= r.val:
p.next = l
l = l.next
p = p.next
else:
p.next = r
r = r.next
p = p.next
else:
break
if l != None:
p.next = l
if r != None:
p.next = r
return head_res.next