-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019-08-15:谈一谈如何判断一个链表成环? #122
Comments
步长分别为1,2遍历队列,如果.next为空就无环,否则两个遍历一定会到同一个节点,也就是有环 |
将所有的遍历过的节点用某个结构存储起来,然后每遍历一个节点,都在这个结构中查找是否遍历过,如果找到有重复,则说明该链表存在循环;如果直到遍历结束,则说明链表不存在循环。 |
用HashMap存储next指向的地址,若map中存在,则有环,否则无环。 |
一个指针A每次走一步,一个指针B每次走二步,假如有环,那么指针B一直比指针A快,一定会有套圈的一刻存在,即指针A和指针B重叠。没有环的话,就是直到指针B指向null,两个指针还没遇上。 |
题目不严谨,应该是如何判断一个单链表成环。 |
莫非单聊表和双链表成环你有不同的判断方法? |
Two ways to solve: /////////////Two Pointers: /////////////////////////
/////////////HashSet:///////////////////////////
|
No description provided.
The text was updated successfully, but these errors were encountered: