
正文
python算法双指针问题:使用列表和数组模拟单链表
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
这个很多基础算法,python已内部实现了。
所以,要想自己实现链表这些功能时,
反而需要自己来构造链表的数据结构。
当然,这是python灵活之处,
也是python性能表达不如意的来源。
value_list = [1, 5, 6, 2, 4, 3]pointer_list = [3, 2, -1, 5, 1, 4]head = 0print(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 = 0pointer = 1linked_list = [[1, 3], [5, 2], [6, -1], [2, 5], [4, 1], [3, 4]]head = 0print(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]
输出结果
123456==================123456Process finished with exit code 0






