题目:
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]
class Solution(object):
def mergeTwoLists(self, list1, list2):
if not list1:
return list2
if not list2:
return list1
res = ListNode()
nn = res
while list1 and list2:
if list1.val < list2.val:
nn.next = list1
list1 = list1.next
elif list1.val >= list2.val:
nn.next = list2
list2 = list2.next
nn = nn.next
nn.next = list1 if list1 else list2
return res.next
链表一下子没想起来该怎么写,思路倒是有.......
标签:21,nn,list1,next,链表,val,list2,LeetCode From: https://blog.51cto.com/u_16123878/6731890