-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist.py
102 lines (84 loc) · 2.04 KB
/
linkedlist.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class Node ():
def __init__(self, value):
self.value = value
self.next = None
def printNodes(self):
print self.value,
print '->',
cur = self.next
while cur:
print cur.value,
print '->',
cur = cur.next
def initalize ():
head = Node(10)
cur = head
for i in range(9,0, -1):
cur.next = Node(i)
cur = cur.next
return head
def reverseLinkedList(head):
if head == None:
return None
if head.next == None:
return head
reverse = reverseLinkedList(head.next)
head.next.next = head
head.next = None
print reverse.value
return reverse
def removeDups(head):
duptracker = {}
cur = head
duptracker[cur]
def assignPartition(node, l):
l.next = node
node.next = None
return node
def partitionList(head, p):
greaterHead = greater = None
lessHead = less = None
equalHead = equal = None
cur = head
while cur.next:
if p == cur.value:
if equalHead == None:
equalHead = cur
equalHead = equal
else:
equal.next = cur
equal = cur
elif p > cur.value:
if greaterHead == None:
greaterHead = cur
greaterHead = greater
else:
greater.next = cur
greater = cur
else:
if lessHead == None:
lessHead = cur
less = lessHead
else:
less.next = cur
less = cur
cur = cur.next
newHead = lessHead
if less:
if equal:
less.next = equalHead
elif greater:
less.next = greaterHead
elif equal:
newHead = equalHead
if greater:
equal.next = greaterHead
else:
newHead = greaterHead
greater.next = None
return newHead
head = initalize()
head.printNodes
print ""
head = partitionList(head, 5)
head.printNodes()