python算法雙指針問題:使用列表和數組模擬單鏈表
阿新 • • 發佈:2019-02-07
ESS 結構 和數 自己 pan 雙指針 pri roc exit
這個很多基礎算法,python已內部實現了。
所以,要想自己實現鏈表這些功能時,
反而需要自己來構造鏈表的數據結構。
當然,這是python靈活之處,
也是python性能表達不如意的來源。
value_list = [1, 5, 6, 2, 4, 3] pointer_list = [3, 2, -1, 5, 1, 4] head = 0 print(value_list[head]) next_pointer = pointer_list[head] while next_pointer != -1: print(value_list[next_pointer]) next_pointer= pointer_list[next_pointer] print(‘==================‘) value = 0 pointer = 1 linked_list = [[1, 3], [5, 2], [6, -1], [2, 5], [4, 1], [3, 4]] head = 0 print(linked_list[head][value]) next_pointer = linked_list[head][next_pointer] while next_pointer != -1: print(linked_list[next_pointer][value]) next_pointer= linked_list[next_pointer][pointer]
輸出結果
1
2
3
4
5
6
==================
1
2
3
4
5
6
Process finished with exit code 0
python算法雙指針問題:使用列表和數組模擬單鏈表