直接读到列表里排序再写回原链表。
# 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]:
lis = []
temp = head
while temp:
lis.append(temp.val)
temp = temp.next
lis = sorted(lis)
temp = head
for i in lis:
temp.val = i
temp = temp.next
return head
还真就