7、合并两个短list
2020-04-02 本文已影响0人
九答
描述
description链表题,要用temp标记起始位置,最后输出temp.next。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
temp = ListNode(0)
l = temp
while l1 and l2:
if l1.val<l2.val:
l.next = l1
l1 = l1.next
else:
l.next = l2
l2 = l2.next
l = l.next
l.next = l1 or l2 #最后的值也要插入进去
return temp.next