-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListSortInPlace.py
75 lines (62 loc) · 1.55 KB
/
LinkedListSortInPlace.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
class Node:
def __init__(self, val, nxt=None):
self.val = val
self.nxt = nxt
def printList(head):
node = head
while node:
print("node: {}".format(node.val))
node = node.nxt
print("")
def insertNode(newNode, newHead):
if newHead == None:
newHead = newNode
return newHead
node = newHead
prevNode = node
while node.nxt and node.nxt.val < newNode.val:
prevNode = node
node = node.nxt
print("{}------->{}".format(newNode.val, node.val))
if node == newHead:
if node.val > newNode.val:
newNode.nxt = node
newHead = newNode
else:
node.nxt = newNode
newHead = node
else:
if not node.nxt:#we are at end
if node.val > newNode.val:
newNode.nxt = node
prevNode.nxt = newNode
else:
node.nxt = newNode
else:#we are at between
newNode.nxt = node.nxt
node.nxt = newNode
printList(newHead)
return newHead
def sortList(head):
node = head
newHead = None
while node:
tnxt = node.nxt
node.nxt = None
newHead = insertNode(node, newHead)
node = tnxt
return newHead
head = None
end = None
for ele in [9, 6, 3, 7, 2, 5, 8, 4, 1]:
if head == None:
node = Node(ele, None)
head = node
end = node
else:
node = Node(ele, None)
end.nxt = node
end = end.nxt
#printList(head)
head = sortList(head)
#printList(head)