LeetCode — Linked List Cycle 解题报告
来源:互联网 发布:网络层ddos 编辑:程序博客网 时间:2024/03/29 17:53
转载请注明:http://blog.csdn.net/ict2014/article/details/17536123
原题如下:
题目解析:
这道题目是“判断一个单链表是否有环?”,最经典的做法就是设定两个指针,一个快指针,一个慢指针,快指针每次走两步,慢指针每次走一步,如果存在环的时候,快慢指针会重合。如果没有环,则会走到链表的末尾。
题目代码:
/** * 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){ return false; } ListNode* slow = head; ListNode* fast = head; while(fast != NULL && fast->next != NULL){ fast = fast->next->next; slow = slow->next; if(slow == fast){ return true; } } return !(fast == NULL || fast->next == NULL); }};
0 0
- LeetCode — Linked List Cycle 解题报告
- [LeetCode OJ] Linked List Cycle 解题报告
- [leetcode] 141. Linked List Cycle 解题报告
- 【LeetCode】Linked List Cycle 解题报告
- [Leetcode] 141. Linked List Cycle 解题报告
- LeetCode — Linked List Cycle II 解题报告
- [LeetCode OJ] Linked List Cycle II 解题报告
- [leetcode] 142. Linked List Cycle II 解题报告
- [Leetcode] 142. Linked List Cycle II 解题报告
- [leetcode]141. Linked List Cycle@Java解题报告
- [leetcode]142. Linked List Cycle II@Java解题报告
- 【LeetCode】141.Linked List Cycle(easy)解题报告
- LeetCode---Linked List Cycle、Linked List Cycle II解题分析
- leetCode Linked List Cycle 解题分享
- leetcode解题报告234——Palindrome Linked List
- [LeetCode]Reverse Linked List II, 解题报告
- [LeetCode]Reverse Linked List II解题报告
- 【LeetCode】Reverse Linked List II 解题报告
- 视频录制
- 在SELS中配置SSH用户等效性时,遇到错误:PRNG is not seeded
- 通用2014职称计算机考试excel2003 word2003 internet题库
- AfxBeginThread
- 人脸识别的现状
- LeetCode — Linked List Cycle 解题报告
- cocos2dx 3.0之判断点击精灵透明区域
- 写sfss时遇到的问题
- codeforce日记 371A
- Struts2(服务器端)输入校验
- 关于图像特征提取
- 组合数学小结
- 单例模式(Singleton Pattern)案例解析
- struts2校验器概述3