解题思路:
- 既然给了空间复杂度就要用起来,创建一个列表
- 对列表排序
- 根据列表创建链表
# 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 not head:
return
lst = []
q = head
while q:
lst.append(q.val)
q = q.next
pre = ListNode(0)
res = pre
lst = sorted(lst)
print(lst)
for i in range(len(lst)):
res.next = ListNode(lst[i])
res = res.next
return pre.next