链表插入排序 | StriveZs的博客

链表插入排序

链表插入排序

这里是链表的插入排序。时间效率和空间效率十分堪忧,但是思想比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class 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
StriveZs wechat
Hobby lead  creation, technology change world.