leecode第一百四十一题(环形链表)

/**
* 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==NULL)
return false; ListNode *fast_node=head;
ListNode *slow_node=head;
do
{
if(slow_node->next!=NULL)//慢指针,一次挪一位
slow_node=slow_node->next;
else
return false; if(fast_node->next!=NULL)//快指针,一次挪两位
fast_node=fast_node->next;
else
return false;
if(fast_node->next!=NULL)//懒的想花里胡哨的方法了
fast_node=fast_node->next;
else
return false; }while(slow_node!=fast_node);//只要有环一定会有两个指针相遇的情况 return true;
}
};
分析:
这个题我也见过,也是剑指offer上的,但是我错了两处,一是忘了边界处理,二是快指针设置不应该依赖慢指针,应该只看自己的。
leecode第一百四十一题(环形链表)的更多相关文章
- Leetcode 142题 环形链表 II(Linked List Cycle II) Java语言求解
题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 p ...
- leecode第一百四十二题(环形链表II)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第一百四十八题(排序链表)
class Solution { public: void sort_list(ListNode* head1, ListNode* head2,int len)//在原链表上进行排序 { ListN ...
- leecode第六十一题(旋转链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第一百四十六题(LRU缓存机制)
class LRUCache { private: unordered_map<int, list<pair<int,int>>::iterator> _m; // ...
- 【leetcode 简单】 第三十五题 环形链表
给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * ...
- Leetcode 141题 环形链表(Linked List Cycle) Java语言求解
题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map ...
- 【leetcode 简单】 第一百一十一题 可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
- 第一百四十一节,JavaScript,封装库--DOM加载
JavaScript,封装库--DOM加载 DOM加载,跨浏览器封装DOM加载,当网页文档结构加载完毕后执行函数,不等待图片音频视频等文件加载完毕 /** dom_jia_zai()函数,DOM页面加 ...
随机推荐
- URL的解析,C语言实现
源: URL的解析,C语言实现 c语言实现urlencode和decode
- Mysql 利用拷贝data目录文件的方式迁移mysql数据库
Mysql 利用拷贝data目录文件的方式迁移mysql数据库 步骤如下: 1.首先要确定data目录 这个问题困扰了我很久,因为网上的帖子大部分只是说拷贝mysql数据库目录下的data文件夹中的数 ...
- Systen,IO
private void CreateHtml(string sPath, string txt) { string currPath = @"C:\MyCodeHelper" + ...
- Codeforces 825D Suitable Replacement - 贪心 - 二分答案
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...
- C#操作字符串方法总结
/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ####### ...
- TCPCopy 使用方法
TCPCopy 使用方法 TCPCopy是一种请求复制(所有基于tcp的packets)工具,可以把在线请求导入到测试系统中去.目前此工具已经广泛应用于国内各大互联网公司. TCPCopy七大功能 1 ...
- centos6.8下搭建编译openwrt的环境
1. 安装必要软件 su root yum install zlib-devel zlib-static -y 2. 编译openwrt 请参考这里
- X-Pack for the Elastic Stack [6.2] » Securing the Elastic Stack »Setting Up User Authentication
https://www.elastic.co/guide/en/x-pack/current/setting-up-authentication.html Active Directory User ...
- Linux command: grep
How to use grep to match multiple strings in the same line? grep 'string1\|string2' filename grep -E ...
- P4008 [NOI2003]文本编辑器
思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...