File tree Expand file tree Collapse file tree 8 files changed +50
-3
lines changed Expand file tree Collapse file tree 8 files changed +50
-3
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,22 @@ def clear(self):
119
119
self .length = 0
120
120
self .tailnode = None
121
121
122
+ def reverse (self ):
123
+ """反转链表"""
124
+ curnode = self .root .next
125
+ self .tailnode = curnode # 记得更新 tailnode,多了这个属性处理起来经常忘记
126
+ prevnode = None
127
+
128
+ while curnode :
129
+ nextnode = curnode .next
130
+ curnode .next = prevnode
131
+
132
+ if nextnode is None :
133
+ self .root .next = curnode
134
+
135
+ prevnode = curnode
136
+ curnode = nextnode
137
+
122
138
123
139
def test_linked_list ():
124
140
ll = LinkedList ()
@@ -169,6 +185,16 @@ def test_linked_list_remove():
169
185
ll .remove (7 )
170
186
print (list (ll ))
171
187
188
+
189
+ def test_linked_list_reverse ():
190
+ ll = LinkedList ()
191
+ n = 10
192
+ for i in range (n ):
193
+ ll .append (i )
194
+ ll .reverse ()
195
+ assert list (ll ) == list (reversed (range (n )))
196
+
197
+
172
198
def test_linked_list_append ():
173
199
ll = LinkedList ()
174
200
ll .appendleft (1 )
@@ -179,3 +205,4 @@ def test_linked_list_append():
179
205
if __name__ == '__main__' :
180
206
test_linked_list ()
181
207
test_linked_list_append ()
208
+ test_linked_list_reverse ()
Original file line number Diff line number Diff line change @@ -123,3 +123,18 @@ def test_heapsort_reverse():
123
123
l = list (range (10 ))
124
124
random .shuffle (l )
125
125
assert heapsort_reverse (l ) == sorted (l , reverse = True )
126
+
127
+
128
+ def heapsort_use_heapq (iterable ):
129
+ from heapq import heappush , heappop
130
+ items = []
131
+ for value in iterable :
132
+ heappush (items , value )
133
+ return [heappop (items ) for i in range (len (items ))]
134
+
135
+
136
+ def test_heapsort_use_heapq ():
137
+ import random
138
+ l = list (range (10 ))
139
+ random .shuffle (l )
140
+ assert heapsort_use_heapq (l ) == sorted (l )
Original file line number Diff line number Diff line change 174
174
< li class ="toctree-l2 "> < a href ="#_7 "> 延伸阅读</ a > </ li >
175
175
176
176
177
+ < li class ="toctree-l2 "> < a href ="#leetcode "> Leetcode</ a > </ li >
178
+
179
+
177
180
</ ul >
178
181
</ li >
179
182
@@ -346,7 +349,7 @@ <h1 id="_5">实现堆排序</h1>
346
349
</ code > </ pre >
347
350
348
351
< h1 id ="python-heapq "> python 里的 heapq</ h1 >
349
- < p > python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档 。</ p >
352
+ < p > python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档并使用内置的 heapq 模块完成堆排序 。</ p >
350
353
< h1 id ="_6 "> 练习题</ h1 >
351
354
< ul >
352
355
< li > 这里我用最大堆实现了一个 heapsort_reverse 函数,请你实现一个正序排序的函数。似乎不止一种方式</ li >
@@ -360,6 +363,8 @@ <h1 id="_7">延伸阅读</h1>
360
363
< li > 《Data Structures and Algorithms in Python》 13.5 节 Heapsort</ li >
361
364
< li > 阅读 Python heapq 模块的文档</ li >
362
365
</ ul >
366
+ < h1 id ="leetcode "> Leetcode</ h1 >
367
+ < p > 合并 k 个有序链表 https://leetcode.com/problems/merge-k-sorted-lists/description/</ p >
363
368
364
369
</ div >
365
370
</ div >
Original file line number Diff line number Diff line change @@ -528,5 +528,5 @@ <h2 id="_20">本电子书制作和写作方式</h2>
528
528
529
529
<!--
530
530
MkDocs version : 1.0.4
531
- Build Date UTC : 2018-12-18 02:39:15
531
+ Build Date UTC : 2018-12-18 09:08:24
532
532
-->
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments