-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertionSortList.py
More file actions
33 lines (30 loc) · 1.05 KB
/
InsertionSortList.py
File metadata and controls
33 lines (30 loc) · 1.05 KB
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
# 147. Insertion Sort List
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None or head.next is None:
return head
dummy = ListNode(0)
dummy.next = head
curr = head
while curr.next:
# 当前值大于下一个值,将下一个值插入到当前值之前的链表中
if curr.val > curr.next.val:
prev = dummy # 从头开始遍历,找到大于下一个值的节点,将下一个值插到前面。
while prev.next.val < curr.next.val:
prev = prev.next
temp = curr.next # temp为待插入的值
# 将curr.next暂时删除
curr.next = temp.next
temp.next = prev.next
prev.next = temp
else:
curr = curr.next
return dummy.next