LeetCode题解——Linked List Cycle
来源:互联网 发布:iphone与mac传照片 编辑:程序博客网 时间:2024/06/05 21:07
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: //想法:用两个指针,一个慢指针,一个快指针,当慢指针追上快指针时,那么有cycle //特殊输入:空链表,两个节点的环形链表,多个节点的环形链表,普通链表 bool hasCycle(ListNode *head) { if(!head) return false; ListNode * fast = head; ListNode * 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 II
- LeetCode题解:Linked List Cycle
- [题解][LeetCode][Linked List Cycle]
- LeetCode题解:Linked List Cycle
- LeetCode题解:Linked List Cycle
- LeetCode Linked List Cycle & Linked List Cycle II题解
- [题解][LeetCode][Linked List Cycle II]
- LeetCode 题解(47): Linked List Cycle
- LeetCode题解:Linked List Cycle II
- Leetcode题解 141. Linked List Cycle
- Leetcode题解 141. Linked List Cycle
- Leetcode题解 142. Linked List Cycle II
- LeetCode 141 Linked List Cycle题解
- leetcode题解-141. Linked List Cycle
- leetcode题解-142. Linked List Cycle II
- leetcode题解-141. Linked List Cycle
- leetcode题解-142. Linked List Cycle II
- 在MathType6.9中出现公式重叠了怎么办?
- CASE WHEN函数
- 讲述Linux HUP信号的干扰问题
- jquery实现指定时间内替换指定链接
- Linux设置ls时不同类型文件的颜色
- LeetCode题解——Linked List Cycle
- 关于Retina技术&分辨率
- 四层和七层负载均衡的区别
- 黑马程序员--网络编程
- iOS开发~CocoaPods使用详细说明
- Linux下部署多个Tomcat
- android 绘图机制源码分析
- js添加导航栏选中样式
- JSP中c:if 标签的使用