class Solution:
def Merge(self , pHead1: ListNode, pHead2: ListNode) :
def create_list(head,lis): #这是一个遍历链表生成列表的函数
while head:
tmp=head.val
lis.append(tmp)
head=head.next
return lis
lis=create_list(pHead1,[])+create_list(pHead2,[]) #列表合并排序
lis.sort()
head=ListNode(0) #生成虚拟头节点
tmp=head #遍历列表生成链表
for i in range(0,len(lis)):
node=ListNode(lis[i])
tmp.next=node
tmp=node
return head.next #虚拟头节点是空的,因此链表开头是他的下一位

京公网安备 11010502036488号