forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
executable file
·197 lines (175 loc) · 4.96 KB
/
node.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
# This is the structure of Node
# Note: there's no need to delare it before the __init__
class Node:
def __init__(self, data):
self.data = data
self.next = None
def create_linked_list(current, data_list):
if isinstance(current, int):
current = Node(current)
for data in data_list:
new_node = Node(data)
current.next = new_node
current = new_node
def print_list(head):
while head is not None:
print head.data
head = head.next
def append_to_tail(head, data):
if head is None:
return Node(data)
current = head
while current.next != None:
current = current.next
current.next = Node(data)
return head
def append_to_head(head, data):
new_head = Node(data)
if head is not None:
new_head.next = head
return new_head
def delete_node(head, data):
while head is not None and head.data == data:
head = head.next
prev = head
current = head.next
while current is not None:
if current.data == data:
prev.next = current.next
else:
prev = prev.next
current = current.next
return head
def remove_duplicate(head):
data_list = [head.data,]
prev = head
head = head.next
while head is not None:
# Found duplicate, delete it
if head.data in data_list:
prev.next = head.next
head = head.next
# Not found, keep go on
else:
data_list.append(head.data)
head = head.next
prev = prev.next
def find_kth(head, k):
current = head
for i in range(k):
current = current.next
while current is not None:
head = head.next
current = current.next
return head.data
def delete_middle_node(node):
if node is None or node.next is None:
return False
node.data = node.next.data
node.next = node.next.next
return True
def partition_list(head, x):
before_head = None
end_head = None
before_list = None
after_lsit = None
while head is not None:
if head.data <= x:
if before_list is None:
before_list = head
before_head = head
else:
before_list.next = head
before_list = before_list.next
else:
if end_list is None:
end_list = head
end_head = head
else:
end_list.next = head
end_list = end_list.next
head = head.next
if before_head is None:
return end_head
before_list.next = end_head
return before_head
def number_add(head1, head2):
carry = 0
prev = None
head = None
while head1 is not None or head2 is not None or carry>0:
result = Node(0)
if head is None:
head = result
if prev is not None:
prev.next = result
if head1 is not None and head2 is not None:
result.data = (head1.data + head2.data + carry) % 10
carry = (head1.data + head2.data + carry) / 10
head1 = head1.next
head2 = head2.next
elif head1 is None and head2 is not None:
result.data = (head2.data + carry) % 10
carry = (head2.data + carry) / 10
head2 = head2.next
elif head2 is None and head1 is not None:
result.data = (head1.data + carry) % 10
carry = (head1.data + carry) / 10
head1 = head1.next
else:
result.data = carry
carry = 0
prev = result
return head
# This one need to confirm wether two lists have the same digits
"""
I'll come back later when I really want to do this stupid thing
def reverse_add(head1, head2):
carry = 0
prev = None
while
"""
def find_begin_loop(head):
slow_runner = head
fast_runner = head
while slow_runner != fast_runner:
slow_runner = slow_runner.next
fast_runner = fast_runner.next.next
# Met
fast_runner = head
while slow_runner != fast_runner:
slow_runner = slow_runner.next
fast_runner = fast_runner.next
return slow_runner
# Later on check Palindom algorithm, pretty stupid in my memory
if __name__ == "__main__":
a = Node(10)
create_linked_list(a, [1,3,3,2,44,5,87,42,42])
print '*'*10
print_list(a)
print '*'*10
append_to_tail(a, 33)
print '*'*10
new_head = append_to_head(a,19)
print_list(new_head)
print '*'*10,'original'
print_list(a)
print '*'*10,'delete'
print_list(delete_node(a,10))
print '*'*10,'remove duplicate'
remove_duplicate(a)
print_list(a)
print '*'*10
print find_kth(a,1)
print '*'*10
head1 = Node(1)
head2 = Node(2)
create_linked_list(head1, [6,4,])
print '*'*10
create_linked_list(head2, [3,8,])
print_list(head1)
print '*'*10
print_list(head2)
print '*'*10
print_list(number_add(head1,head2))