Lab08 题解 (UCB CS61A@2021 Fall)
Q2: Convert Link
Write a function
convert_link
that takes in a linked list and returns the sequence as a Python list. You may assume that the input list is shallow; that is none of the elements is another linked list.Try to find both an iterative and recursive solution for this problem!
迭代的算法很简单, 我们只要创建一个 result
list 来存储结果, 遍历链表的同时记住访问过的数即可