leetcode147对链表进行插入排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014204761/article/details/82933660
def insertionSortList(self, head):
        """
        对链表进行插入排序,单链表没有前驱指针只能从前往后,添加一个辅助有序链表,依次从前比较
        """
        new_head = ListNode(0) ###新链表的头指针
        new_t = new_head
        cur = head
        while cur:
            new_head = new_t
            while new_head.next and new_head.next.val <= cur.val:  ###遍历辅助有序链表直到满足条件
                new_head = new_head.next
            t = cur.next
            cur.next = new_head.next
            new_head.next = cur
            cur = t
        return new_t.next

猜你喜欢

转载自blog.csdn.net/u014204761/article/details/82933660