# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
def mergeTwoLists(l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
还是单链表呗
"""
l3=ListNode()
l4=l3
while l1!=None and l2!=None:#l1没到头。comp l1 l2,insert
if l1.val<=l2.val:
l3.next=l1
l3=l3.next
l1=l1.next
else :
l3.next=l2
l3=l3.next
l2=l2.next
if l1==None:
l3.next=l2
else:
l3.next=l1
return l4.next
head=ListNode(-10**4)
for node_index in range(len(lists)):
head=mergeTwoLists(head,lists[node_index])
return head.next
今天两道啦!