题解 | #单链表的排序#
单链表的排序
http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
辅助数组排序
1.遍历链表,转化为数组
2.对数组排序
3.将数组又转化为链表
class Solution:
def sortInList(self , head: ListNode) -> ListNode:
# write code here
num_list = []
while head:
num_list.append(head.val)
head = head.next
num_list.sort()
N0 = ListNode(-1)
cur = N0
for v in num_list:
cur.next = ListNode(v)
cur = cur.next
return N0.next