链表问题可以考虑哑节点(相当于就是头结点,或者一个哨兵)。
思路二:递归
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
cur = dummy_head = ListNode(-1)
while l1 and l2:
if l1.val > l2.val:
cur.next = l2
l2 = l2.next
cur = cur.next
else:
cur.next = l1
l1 = l1.next
cur = cur.next
if l1:
cur.next = l1
elif l2:
cur.next = l2
else:
cur.next = None
return dummy_head.next
原文:https://www.cnblogs.com/zzychage/p/14998802.html