这个很多基础算法, 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
来源: http://www.bubuko.com/infodetail-2946835.html