141. Linked List Cycle
来源:互联网 发布:淘宝产品模板详情代码 编辑:程序博客网 时间:2024/06/08 19:10
快慢指针的应用。慢指针每次一步,快指针每次两步。要是有环的话终究是要相遇的。
bool hasCycle(ListNode *head) { ListNode*slow=head,*fast=head; if(!head) return false; while(fast){ slow=slow->next; fast=fast->next; if(fast) fast=fast->next; else return false; if(fast==slow) return true; } return false; }
bool hasCycle(ListNode *head) { ListNode*slow=head,*fast=head; while(fast&&fast->next){ slow=slow->next; fast=fast->next->next; if(fast==slow) return true; } return false; }
bool hasCycle(ListNode *head) { ListNode*slow=head,*fast=head; while(fast&&fast->next&&fast->next->next){ slow=slow->next; fast=fast->next->next->next; if(fast==slow) return true; } return false; }
跑的快的终极是要追上跑的慢的,理想情况下,快的是慢的2倍时,当慢的跑完一次全程,快的已经跑完2次的全程。
阅读全文
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- Linux on Power 上的调试工具和技术
- Tomacat9.0 报错 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'so
- 一元三次方程求解
- web前端性能优化总结
- canvas保存页面为图片传入服务器的小问题
- 141. Linked List Cycle
- 多态 与 虚函数
- 无限极折叠分类
- 什么是React?
- redis-5大对象之---存储基础结构
- 邮件安全的攻守之道
- Python笔记之psutil模块(自动化运维利器psutil玩转系统监控及管理)
- 20171215Link
- recyclerview 利用Timer实现自动滚动