141. Linked List Cycle
来源:互联网 发布:android仿淘宝首页 编辑:程序博客网 时间:2024/05/16 04:54
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Subscribe to see which companies asked this question
注意加一个runner->next!=NULL
代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { if(head==NULL || head->next==NULL ||head->next->next==NULL) return false; ListNode* walker=head->next; ListNode* runner=head->next->next; while(walker!=NULL && runner!=NULL&& runner->next!=NULL) { if(walker==runner) return true; walker=walker->next; runner=runner->next->next; } return false; }};
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
- 面试题25:二叉树中和为某一值的路径
- 共享一个右侧滑出的自动消失的提示框
- 事件处理机制之监听EditText的内容变化
- springmvc的Controller并发
- 关于建站和域名的杂谈
- 141. Linked List Cycle
- SQL Server 对XML数据功能支持
- h5+css 水平菜单制作
- 【bzoj2055】【80人环游世界】【有上下界的费用流】
- 多重继承与void*指针转换问题的分析
- 在Ubuntu安装NodeJS后,不能使用node npm命令
- github atom编辑器前端开发实战(一)- 一键用 chrome 浏览器打开当前编辑的文件,亲测可用
- storm中 worker executor task之间的关系
- MongoDB c++ driver(一)