其实可能是python的list用多了的缘故,会习惯性用sort()和sorted()来排序,注意sort()没返回值, sorted()有返回值,
所以自然就想到把链表里的数据放出来装到list里再排序,再装回一个新链表
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
if pHead1 == None and pHead2 == None:
return pHead1 or pHead2
else:
stack1 = []
stack2 = []
stack = []
res = ListNode(0)
dummy = res
while pHead1:
stack1.append(pHead1.val)
pHead1 = pHead1.next
while pHead2:
stack2.append(pHead2.val)
pHead2 = pHead2.next
stack = sorted(stack1 + stack2)
for i in stack:
dummy.next = ListNode(i)
dummy = dummy.next
return res.next
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
if pHead1 == None and pHead2 == None:
return pHead1 or pHead2
else:
stack1 = []
stack2 = []
stack = []
res = ListNode(0)
dummy = res
while pHead1:
stack1.append(pHead1.val)
pHead1 = pHead1.next
while pHead2:
stack2.append(pHead2.val)
pHead2 = pHead2.next
stack = sorted(stack1 + stack2)
for i in stack:
dummy.next = ListNode(i)
dummy = dummy.next
return res.next