![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
快慢指针
只会写臭虫
这个作者很懒,什么都没留下…
展开
-
如何寻找单链表的中间节点(快慢指针)
设置两个指针,一个快指针,每次走两步,一个慢指针,每次走一步,直到快指针走到链尾。 public class searchMid { public Node method(Node head) { Node p=head; Node q=head; while(q!=null&&q.next!=null&&q.next.next!=null) {...原创 2019-08-15 14:55:54 · 342 阅读 · 0 评论 -
LeetCode 141.链表判环, LeetCode 142.并找出环的入口
判环利用快慢指针,若快指针遍历完链表之前快慢指针没有相遇的话,则说明链表中有环。 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * };...原创 2019-08-17 00:24:04 · 335 阅读 · 0 评论