-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path147.py
31 lines (30 loc) · 836 Bytes
/
147.py
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
# 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
"""
if not head:
return head
res = ListNode(-float('inf'))
res.next = head
pre = head
tag = head.next
while tag:
if tag.val < pre.val:
pre.next = tag.next
pre1 = res
while tag.val > pre1.next.val:
pre1 = pre1.next
tag.next = pre1.next
pre1.next = tag
tag = pre.next
continue
tag = tag.next
pre = pre.next
return res.next