leetCode-linkedListCycle判断链表是否有环
题目
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
分析
判断链表是否有环,采用快慢指针,如果相遇则表示有环
AC代码
/**
* 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 || !head->next){
return false;
}
ListNode* slow = head;
ListNode* fast = head->next;
while(fast->next && fast->next->next && fast != slow){
fast = fast->next->next;
slow = slow->next;
}
return fast == slow;
}
};
leetCode-linkedListCycle判断链表是否有环的更多相关文章
- LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [leetcode]141. Linked List Cycle判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- 判断链表是否有环(Java实现)
判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- python判断链表是否有环
思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点: 假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点 ( ...
随机推荐
- Flink - ShipStrategyType
对于DataStream,可以选择如下的Strategy, /** * Sets the partitioning of the {@link DataStream} so that the ou ...
- Flash builder 、flash cs6、 as 3.0研究
1.Flash/Actionscript3 载入资源文件方法考 http://zengrong.net/post/1107.htm 2.使用Flash Professional CS5和Flash B ...
- 终于碰到iOS对象集合深拷贝的坑
从原始数组,拆分排列组合成新数组,同时给新的数组中的模型元素追加字段,数组的容量翻倍,如果不用深拷贝,后面追加的值就把前面的值覆盖了 UnitModel *model1 = [UnitModel ne ...
- MonkeyRunner_真机_运行脚本(二)
# -*- coding: UTF-8 -*- #手机分辨率为1080*1920 import sys from com.android.monkeyrunner import MonkeyRunne ...
- Maven之基本概念及特性的基本介绍
maven最主要的概念是坐标和依赖,这是maven可以极大简化构建过程以及进行项目管理的基础. 坐标 类似于地理位置的坐标,maven的坐标也是用来标记的,不同是它是来标记maven中的不同组件,也就 ...
- flex布局 响应式布局
移动端页面开发流程 移动端页面布局 一.移动端app分类 1.Native App原生app手机应用程序 使用原生的语言开发的手机应用,Android系统用的是java,ios系统用的是objec ...
- 连连看java版
主界面 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt. ...
- 3.0-uC/OS-III简介(操作系统结构)
1.OS-III是一个第 3代的系统内核,支持现代的实时内核所期待的大部分功能. 例如资源管理, 同步, 任务间的通信等等.然而, uC/OS-III提供的特色功能在其它的实时内核中是找不到的, 比如 ...
- .NET Core 使用 Kestrel
Kestrel介绍 Kestrel是一个基于libuv的跨平台web服务器 在.net core项目中就可以不一定要发布在iis下面了 Kestrel体验 可以使用useUrls来设置一个请求的地址 ...
- SQL优化之踩过的坑【一】
正看资料看的过瘾,突然收到报警,说服务器负载太高,好吧,登录服务器看看,我擦嘞,还能不能愉快的玩耍了?下面是当时的负载情况 看见mysql使用cpu已经到了2000,io没有等待.说明应该没有大的临时 ...