链表插入排序 Posted on 2021-07-31 | In 链表 , 算法 | | 链表插入排序 这里是链表的插入排序。时间效率和空间效率十分堪忧,但是思想比较简单。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# Definition for singly-linked list.class ListNode(object): def __init__(self, val=0, next=None): self.val = val self.next = nextclass Solution(object): def insertionSortList(self, head): """ 插入排序:除了第一个节点不动,第二个元素和前面已经排序好的元素进行比较,插入到合适的位置 重复上述过程即可。 比如: 1 3 5 2 4 |左边为排序好的节点 1 | 3 5 2 4 1 3 | 5 2 4 1 3 5 | 2 4 1 2 3 5 | 4 1 2 3 4 5 | :type head: ListNode :rtype: ListNode """ if head.next is None: return head ends = head.next node = head.next start = head # 单独处理前两个节点 if node.val < start.val: start.next = node.next node.next = start ends = start start = node head = node # 递归处理后面所有情况 node = ends.next while node is not None: # 寻找合适的插入位置 (3种情况:头插入、中间插入、尾插入) # 头插入 if node.val < start.val: ends.next = node.next node.next = start start = node head = node node = ends.next # 尾插入 elif node.val > ends.val: ends = node node = ends.next # 中间插入 else: cur = start # 寻找插入位置 while True: if node.val >= cur.val and node.val <=cur.next.val: break cur = cur.next # 插入 ends.next = node.next node.next = cur.next cur.next = node node = ends.next return head Hobby lead creation, technology change world. Post author: StriveZs Post link: www.strivezs.com/2021/07/31/%E9%93%BE%E8%A1%A8%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.