题解 | #单链表的排序#
单链表的排序
http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类 the head node
# @return ListNode类
#
class Solution:
def sortInList(self , head: ListNode) -> ListNode:
# write code here
if head is None:return None
temp=[]
while head:
temp.append(head.val)
head=head.next
# 冒泡排序
# for i in range(len(temp)):
# for j in range(0,len(temp)-i-1):
# if temp[j]>temp[j+1]:
# a=temp[j]
# temp[j]=temp[j+1]
# temp[j+1]=a
# 使用内部的sort就可以 使用上边的冒泡排序就不行
temp.sort(reverse=False) #reverse=True
head0=pre=ListNode(0)
for index,i in enumerate(temp):
pre.val=i
if index!=len(temp)-1:
pre.next=ListNode(0)
pre=pre.next
return head0