141. Linked List Cycle
来源:互联网 发布:华藏卫视网络直播 编辑:程序博客网 时间:2024/05/10 21:17
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
这道题用两个指针指向表头,一个每次走一步,一个每次走两步,如果这个链表有环的话,那么快的指针会赶上慢的指针。
程序要防止指针为空的情况。
class Solution {public: bool hasCycle(ListNode *head) { ListNode *slow=head; ListNode *fast=head; while(slow&&fast&&fast->next!=NULL) { slow=slow->next; fast=fast->next->next; if(slow==fast) return true; } 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
- 《天符經》
- 二叉树的遍历与求深度的递归与非递归实现
- python文件类型
- Android之旅——04
- 安卓基础:对话框的简单使用
- 141. Linked List Cycle
- js 常用写法笔记
- 安卓基础:广播接收者
- 计算三维空间中点到三角形平面的最短距离
- caffe中多GPU的使用方法
- EL表达式取字符问题
- POJ 2253 - Frogger(dijkstra变形)
- 欢迎使用CSDN-markdown编辑器
- Codeforces 626D. Jerry's Protest