[LeedCode OJ]#141 Linked List Cycle
来源:互联网 发布:桌面整理软件推荐 编辑:程序博客网 时间:2024/06/05 17:28
【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目链接:https://leetcode.com/problems/linked-list-cycle/
题意:
给定一个链表,判断这个链表是否有环
思路:
设定快慢指针,快指针一次走两步,慢指针一次走一步,如果快指针到达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) return false; ListNode *fast,*slow; fast = slow = head->next; while(fast!=NULL&&fast->next!=NULL) { fast = fast->next->next; slow = slow->next; if(fast==slow) return true; } return false; }};
0 0
- [LeedCode OJ]#141 Linked List Cycle
- [LeedCode OJ]#142 Linked List Cycle II
- LeedCode:Linked List Cycle II
- OJ 141 --Linked List Cycle
- [LeedCode OJ]#206 Reverse Linked List
- [LeedCode OJ]#234 Palindrome Linked List
- [LeedCode OJ]#203 Remove Linked List Elements
- LeetCode OJ:Linked List Cycle
- [LeetCode OJ]Linked List Cycle
- LeetCode OJ Linked List Cycle
- [LeedCode]Find the Duplicate Number/Linked List Cycle II
- 141Linked List Cycle
- #141 Linked List Cycle
- 141 Linked List Cycle
- [141]Linked List Cycle
- 141Linked List Cycle
- 141-Linked List Cycle
- 141 Linked List Cycle
- WEB前段助手 FeHelper
- TCP的发送系列 — 发送缓存的管理(二)
- 冒泡排序(三)
- 断舍离2
- Stanford机器学习---第八讲. 支持向量机SVM
- [LeedCode OJ]#141 Linked List Cycle
- Nqueen2.0
- CentOS 5.5下安装MySQL 5.5全过程分享
- 归并排序(四)
- 申请并返回堆空间上的二维空间
- C++迭代器失效: 典型的迭代器失效.
- 如何使用Android命令
- Stanford机器学习---第九讲. 聚类
- Velocity Conference Santa Clara 2015: Complete Video Compilation