Leetcode每日一题
题目链接: 148. 排序链表
难度: 中等
解题思路: 将链表转化为列表进行排序,排序后重新赋值到链表
题解:
# 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: ListNode) -> ListNode:
if head == []:
return []
node = head
arr = []
while node:
arr.append(node.val)
node = node.next
arr.sort()
# print(arr)
node = head
for i in range(len(arr)):
node.val = arr[i]
node = node.next
return head