leetcode 141、Linked list cycle
一种方法是用set存储出现过的指针,重复出现时就是有环:
class Solution {
public:
bool hasCycle(ListNode *head) {
set<ListNode*> st;
ListNode *p = head;
while(p) {
if (st.find(p) != st.end())
return true;
st.insert(p);
p = p->next;
}
return false;
}
};
另一种方法就是快慢指针,快指针一次走两步,慢指针一次走一步。无环时快指针会走向空指针,有环时快指针会赶上慢指针。
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast, *slow;
fast = slow = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (fast == slow)
return true;
}
return false;
}
};
leetcode 141、Linked list cycle的更多相关文章
- [LeetCode] 141&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- leetcode 141 142. Linked List Cycle
题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- <LeetCode OJ> 141 / 142 Linked List Cycle(I / II)
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(Java实现)
这是悦乐书的第176次更新,第178篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第35题(顺位题号是141).给定一个链表,确定它是否有一个循环. 本次解题使用的开发工 ...
随机推荐
- Codeforces - 151C 质因子分解
显然只需要能跑到第二个因子就赢了 需要特判非平凡因子 常数优化:不用求出所有因子,跑完第二个素数就行了 #include<bits/stdc++.h> using namespace st ...
- SpringBoot2.0.3整合Quartz2.3.0实现定时任务
转载:https://www.cnblogs.com/ealenxie/p/9134602.html 关于别人写的quartz学习的地址:https://blog.csdn.net/lkl_csdn/ ...
- python__画图表可参考(转自:寒小阳 逻辑回归应用之Kaggle泰坦尼克之灾)
出处:http://blog.csdn.net/han_xiaoyang/article/details/49797143 2.背景 2.1 关于Kaggle 我是Kaggle地址,翻我牌子 亲,逼格 ...
- Win10家庭版组策略gpedit.msc的问题
大家都认为,Windows家庭版中并不包含组策略,其实不然,它是有相关文件的,只是不让你使用而已.那么我们让系统允许你使用就好了.首先你需要在桌面上新建一个txt文本文档.然后将以下代码复制到这个新建 ...
- ORA-1000的问题 Cursor 过多 (文档 ID 18591.1)
#查看用户cursor的使用情况 col sid for a9999999999 col osuser for a20 col machine for a20 col num_curs for a ...
- SpringMVC中的视图和视图解析器
对于控制器的目标方法,无论其返回值是String.View.ModelMap或是ModelAndView,SpringMVC都会在内部将它们封装为一个ModelAndView对象进行返回. Spri ...
- 浅谈jrebel
有个同事提高个jrebel的工具,提起tomcat的热部署方案. jrebel是一款收费的JVM级的热部署工具包. JVM级的热部署也就是说,可以不重启JVM,让修改或添加的类加载到JVM中. 加载器 ...
- jq二级目录
CSS:.qsc_nav_main .level1 { text-align: center; height: auto; } .qsc_nav_main .level1 a { display: i ...
- Unity 组件.name
组件.name 指的是组件所在游戏对象的名字,例如: Animation m_animation; m_animation =GetComponent<Animation>(); m_a ...
- [转]JS 只能输入数字和两位小数的JS
本文转自:http://blog.sina.com.cn/s/blog_724008890101dgep.html JS代码: <script language="JavaScript ...