在Codewar上刷题碰到检测链表里环的长度(必定有环),构思出一个很短的函数。
在代码处一目了然
def loop_size(node): past, length = {}, 0 while past.get(node, None) is None: past[node], length, node = length, length + 1, node.next return length - past[node]
dict真是一个十分好用的数据结构。
上一篇:Python: 今天你赋值了吗?
下一篇:深入理解Python中的ThreadLocal变量(中)