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):
119119 self .length = 0
120120 self .tailnode = None
121121
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+
122138
123139def test_linked_list ():
124140 ll = LinkedList ()
@@ -169,6 +185,16 @@ def test_linked_list_remove():
169185 ll .remove (7 )
170186 print (list (ll ))
171187
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+
172198def test_linked_list_append ():
173199 ll = LinkedList ()
174200 ll .appendleft (1 )
@@ -179,3 +205,4 @@ def test_linked_list_append():
179205if __name__ == '__main__' :
180206 test_linked_list ()
181207 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():
123123 l = list (range (10 ))
124124 random .shuffle (l )
125125 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 174174 < li class ="toctree-l2 "> < a href ="#_7 "> 延伸阅读</ a > </ li >
175175
176176
177+ < li class ="toctree-l2 "> < a href ="#leetcode "> Leetcode</ a > </ li >
178+
179+
177180 </ ul >
178181 </ li >
179182
@@ -346,7 +349,7 @@ <h1 id="_5">实现堆排序</h1>
346349</ code > </ pre >
347350
348351< h1 id ="python-heapq "> python 里的 heapq</ h1 >
349- < p > python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档 。</ p >
352+ < p > python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档并使用内置的 heapq 模块完成堆排序 。</ p >
350353< h1 id ="_6 "> 练习题</ h1 >
351354< ul >
352355< li > 这里我用最大堆实现了一个 heapsort_reverse 函数,请你实现一个正序排序的函数。似乎不止一种方式</ li >
@@ -360,6 +363,8 @@ <h1 id="_7">延伸阅读</h1>
360363< li > 《Data Structures and Algorithms in Python》 13.5 节 Heapsort</ li >
361364< li > 阅读 Python heapq 模块的文档</ li >
362365</ ul >
366+ < h1 id ="leetcode "> Leetcode</ h1 >
367+ < p > 合并 k 个有序链表 https://leetcode.com/problems/merge-k-sorted-lists/description/</ p >
363368
364369 </ div >
365370 </ div >
Original file line number Diff line number Diff line change @@ -528,5 +528,5 @@ <h2 id="_20">本电子书制作和写作方式</h2>
528528
529529<!--
530530MkDocs version : 1.0.4
531- Build Date UTC : 2018-12-18 02:39:15
531+ Build Date UTC : 2018-12-18 09:08:24
532532-->
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments