leetcode - Linked List Cycle
题目:Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
个人思路:
1、判断一个链表是否有环,标准做法是采取快慢指针,一个走一步,一个走两步,当快指针追上慢指针时,表明有环
2、要注意几个地方,1、空节点链表无环 2、快指针的两步也是一步一步走的,每走一步都得进行检查
代码:
1 #include <stddef.h> 2 3 struct ListNode 4 { 5 int val; 6 ListNode *next; 7 ListNode(int x) : val(x), next(NULL) {}; 8 }; 9 10 class Solution 11 { 12 public: 13 bool hasCycle(ListNode *head) 14 { 15 if (!head) 16 { 17 return false; 18 } 19 20 ListNode *one = head; 21 ListNode *two = head; 22 23 while (true) 24 { 25 two = two->next; 26 if (!two) 27 { 28 return false; 29 } 30 if (two == one) 31 { 32 return true; 33 } 34 two = two->next; 35 if (!two) 36 { 37 return false; 38 } 39 if (two == one) 40 { 41 return true; 42 } 43 one = one->next; 44 } 45 } 46 }; 47 48 int main() 49 { 50 return 0; 51 }
View Code
网上基本都是这种方法,主要问题是要考虑特殊情形和边界条件
转载于:https://www.cnblogs.com/laihaiteng/p/3809492.html
leetcode - Linked List Cycle相关推荐
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- LeetCode Linked List Cycle II(floyd cycle)
问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...
- LeetCode | Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode] Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- LeetCode Linked List Cycle II
题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null 思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始 ...
- LeetCode Linked List Cycle
题意:判断链表中是否有环 思路:从起点开始,一个每次移动一步,另一个每次移动两步,如果相遇,说明有环 代码如下: class Solution {public boolean hasCycle(Lis ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)
Time:2019/4/7 Title: Linked List Cycle Difficulty: Easy Author:小鹿 题目:Linked List Cycle I Given a lin ...
最新文章
- Java短连接生成工具-思路
- java中bjt和utc转化_如何在Java中转换UTC和本地时区
- Android网络连接的两种方法:apache client和httpurlconnection的比较
- 计算机主机图片开机按钮,解决电脑开机按钮无法使用方法
- postgresql如何让主键自增
- js获取页面 窗口的宽高
- 【车间调度】基于matlab模拟退火算法求解车间调度(jobshop-3)问题【含Matlab源码 1082期】
- python win32api sendmessage_python win32api模拟后台鼠标点击的问题。
- java入门循序渐进
- 4、keygen激活secureCRT
- 《数据通信与网络》笔记--广域网SONET/SDH
- 小学生python趣味编程-【少儿编程】python趣味编程第二课:写文字
- js获取ISO8601规范时间,使用UTC时间,格式为:YYYY-MM-DDThh:mm:ssZ
- [精]Oracle 内存结构详解
- jquery-js 美化页面
- 一花一树一城,走进三维重建的绚丽世界|专访权龙
- qt---crc32
- 解决sed -i报错:
- 第二十七篇 网页数据解析三种方法: 正则表达--BeautifulSoup--xpath 满满的干货
- 关于vue2停止维护,vue3迭代更新的思考:
热门文章
- [转载]Manually configuring Microsoft Internet Information Services (IIS)
- nodejs-- vuex中mapActions
- 中小企业网络安全提升
- 黄文俊:Serverless小程序后端技术分享
- 去除vue项目中的#及其ie9兼容性
- [已解决]Vistual Stdio 2015 installer Bootstrapper Packages 路径
- 我的Android进阶之旅------Android利用温度传感器实现带动画效果的电子温度计
- 【svn】设置过滤文件
- 安装iproute2
- ActiveReports 报表应用教程 (8)---交互式报表之动态过滤