-
Notifications
You must be signed in to change notification settings - Fork 2
/
Insertion-Sort-List.py
33 lines (30 loc) · 968 Bytes
/
Insertion-Sort-List.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
32
33
class Solution(object):
def insertionSortList(self, head):
def insert(listVal, val):
found = False
for i in range(len(listVal)):
if (listVal[i] > val):
listVal.insert(i, val)
found = True
break
if (found == False):
listVal.append(val)
return listVal
headVals = []
while head:
headVals.append(head.val)
head = head.next
j = list(headVals)
if (len(headVals) == 0):
return None
listVals = [headVals.pop(0)]
for i in range(len(headVals)):
listVals = insert(listVals, headVals[0])
headVals.pop(0)
def fill(root):
if (len(listVals) == 0):
return
head = ListNode(listVals.pop(0))
head.next = fill(root)
return head
return fill(head)