-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path147.InsertionSortList.py
More file actions
56 lines (51 loc) · 1.6 KB
/
147.InsertionSortList.py
File metadata and controls
56 lines (51 loc) · 1.6 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
:note:
插入排序,跟数组不一样,从前往后找插入位置,需要记录插前、插入节点的位置以及当前和前一个节点位置
"""
#head = ListNode(-1)
#head.next = ListNode(5)
#head.next.next = ListNode(3)
#head.next.next.next = ListNode(4)
#head.next.next.next.next = ListNode(0)
pref = None
first = head
if head == None:
return head
prec = head
cur = head.next
while(cur):
while(cur.val >= first.val):
pref = first
first = first.next
if first == cur:
break
curNext = cur.next
if first != cur: # swap
#print pref.val, first.val, prec.val, cur.val
if pref == None:
head = cur
else:
pref.next = cur
cur.next = first
prec.next = curNext
else:
prec = cur
pref = None
first = head
cur = curNext
#self.printL(head)
return head
def printL(self, head):
while(head):
print head.val,
head = head.next
print ''