题目:

思路:
借助list
-
新建list()
-
遍历链表,把数字加到list中
-
调用list的排序函数进行排序
-
把排序后的元素放到链表中
Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
new_list = list()
curr = head
while curr:
new_list.append(curr.val)
curr = curr.next
new_list.sort()
dump = ListNode(-1,head)
temp = dump
for num in new_list:
new_node = ListNode(num)
temp.next = new_node
temp = temp.next
return dump.next