LeetCode141. Linked List Cycle
来源:互联网 发布:adobe官网软件下载 编辑:程序博客网 时间:2024/06/05 22:10
题目链接
题意:判断链表中有无环,不使用额外的存储空间。
这个题很有意思,把链表想象成一条公路的话,一辆车和一个人从起点同时出发,如果有环的话他们一定会相遇。
这里用两个指针代表车和人,车的速度是2,人的速度是1。
class Solution {public: bool hasCycle(ListNode *head) { ListNode *a = head, *b = head; if(head==NULL || head->next==NULL || head->next->next == NULL) return 0; while(1){ a = a->next; if(a==NULL) return 0; a = a->next; if(a==NULL) return 0; b = b->next; if(a==b) return 1; } }};
0 0
- LeetCode141:Linked List Cycle
- leetcode141. Linked List Cycle
- leetcode141: Linked List Cycle
- leetcode141 Linked List Cycle
- LeetCode141. Linked List Cycle
- leetcode141. Linked List Cycle
- leetcode141~Linked List Cycle
- LeetCode141. Linked List Cycle
- leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- leetcode141. Linked List Cycle
- LeetCode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- Leetcode141. Linked List Cycle
- LeetCode141—Linked List Cycle
- LeetCode141之Linked List Cycle java题解
- Leetcode141 - Linked List Cycle(链表)
- UML之类图分析
- LeetCode75. Sort Colors
- Spring MVC 自学杂记(一)
- LeetCode2. Add Two Numbers
- 记录在使用react-native-echarts中遇到的问题
- LeetCode141. Linked List Cycle
- LeetCode160. Intersection of Two Linked Lists
- 技术文章url
- Java 基础 字符串 方法
- Android4.4深入浅出之SurfaceFlinger总体结构
- Centos7 修改运行级别
- 生成gtest静态库 libgtest.a文件
- // OC 中的 getter setter
- LeetCode257. Binary Tree Paths(dfs)