Leetcode 线性表 Linked List Cycle
来源:互联网 发布:阿里云服务器端口管理 编辑:程序博客网 时间:2024/06/16 07:24
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Linked List Cycle
Total Accepted: 17041 Total Submissions: 48975Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:判断一个链表中是否有环
思路:快慢指针,如果有环,最终快慢指针会在非NULL相遇
注:用到fast->next前先要确保fast非NULL,要用fast->next->next前先要确保fast,fast->next非NULL
复杂度:时间O(n), 空间O(1)
相关题目:Linked List CycleII/** * 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) { ListNode *fast, *slow; fast = slow = head; while(fast && fast->next){ fast = fast->next->next; slow = slow->next; if(fast == slow) return true; } return false; }
0 0
- Leetcode 线性表 Linked List Cycle
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode: Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode] - Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode]Linked List Cycle
- 【LeetCode】Linked List Cycle
- Linked List Cycle - LeetCode
- 【LeetCode】Linked List Cycle
- svn 服务器端同步钩子代码
- Android4.4 PackageManagerService详细分析
- 常量
- http://bbs.csdn.net/topics/370159572
- Qt跨平台与跨windows版本
- Leetcode 线性表 Linked List Cycle
- 强制类型转换
- 面向对象之继承
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Autodesk 首届编程马拉松 ( Hackathon ) 活动, 6.14~15 上海
- 关于CRecordset::Open(vs2010)
- 题目1008:最短路径问题
- 费用报销模块设计
- android专业技术网站