147.对链表进行插入排序

难度:中等
题目描述:
147.对链表进行插入排序_第1张图片
思路总结:为什么链表题中等难度的都能做出来,而其它类型的题不行呢,值得思考。
题解一:

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        dummy = ListNode(0)
        while head:
            cur = dummy
            nxt = head.next
            while cur:
                if not cur.next:
                    cur.next = head
                    head.next = None
                elif head.val < cur.next.val:
                    tmp = cur.next
                    cur.next = head
                    head.next = tmp
                    break
                cur = cur.next
            head = nxt
        return dummy.next

题解一结果:
147.对链表进行插入排序_第2张图片

你可能感兴趣的:(朱滕威的面试之路)