思路:
这也太想直接用列表排序了(面试官:回去等通知吧)
思路题目已经明确了
对未排序部分循环插入已排序部分
用vis和unvis来表示两部分
记得将vis和unvis之间的链接打断
速度很慢,应该可以在插入的时候加一个直接到末位的判断,可能会减少很多时间
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
vis=head
unvis=head.next
vis.next=None
def plugin(node,head):
head0=head
while(head):
if node.val<head.val:
node.next=head
head0=node
break
if head.next:
if node.val>=head.val and node.val<=head.next.val:
node.next=head.next
head.next=node
break
else:
head=head.next
else:
head.next=node
break
return head0
while(unvis):
temp=unvis
unvis=unvis.next
temp.next=None
vis=plugin(temp,vis)
return vis