Linked List, Hash Table, Two Pointers [linked list cycle]

Use a hash table to check whether a node had been visited before.

go through 每一个node, 在hash table里 记录node's reference 或者 memory address。如果当前的node 是null, 那么我们就到达了the end of the list, 并且 这个list must not be cyclic. 如果当前node's reference 已经在hash table里面,那么这个list 就是cycle.

技巧:

1, while(hashset.add(n)), 判断hashset能否add一个存在的item。n 已经存在, false。n不存在, True。

2, Floyd's cycle-finding method:Linked List, Hash Table, Two Pointers [linked list cycle]