15. Linked List Cycle && 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 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:
bool hasCycle(ListNode *head) {
ListNode *p1, *p2;
p1 = p2 = head;
while(p2 && p2->next && p2->next->next) {
p2 = p2->next->next;
p1 = p1->next;
if(p1 == p2) return true;
}
return false;
}
};
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up: Can you solve it without using extra space?
说明:在上题基础上,将一个指针放到链表头,步长都设为1,相遇节点。(可以计算)
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *p1, *p2;
p1 = p2 = head;
while(p2 && p2->next && p2->next->next) {
p2 = p2->next->next;
p1 = p1->next;
if(p1 == p2) {
p1 = head;
while(p1 != p2) {
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
}
return NULL;
}
};
15. Linked List Cycle && Linked List Cycle II的更多相关文章
- 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 & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- LeetCode之旅(15)-Odd Even Linked List
题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
随机推荐
- stopPropagation, preventDefault 和 return false
e.stopPropagation()此方法用于阻止事件冒泡或者事件捕获.IE8及以下中没有此方法,使用e.cancelBubble=false 来阻止事件冒泡. 当标准W3C中,事件包括捕获阶段和冒 ...
- IDE启动报错
启动IDE的时候报出org.eclipse.swt.SWTException: Invalid thread access解决方法:退出再次启动IDE的时候加上-clean选项,如图中所示,前面是我的 ...
- 更换内核后重编virtualbox内核模块
这些天编译了一个4.1.15内核,因此vb原来的模块就不能用了,因此要重新编译(当然,reinstall也可以,觉得大动干戈,不符合个人做事风格) 如果不重编运行会有如下错误提示: # virtual ...
- php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊
php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊]]] 还有万网那些域名要备案,备案,备案中...................wqnmlgb 今天摩托车的前后轮被扎了,tnn ...
- 开发经验之状态机思想,分别使用了swift,OC,C,PHP语言实现
这里设计一个简单的练习,使用状态机思想实现,分别使用了swift,OC,C,PHP语言实现 题目:1到10000遍历,开始-打印奇数-遇到7的倍数开始打印偶数--遇到10的倍数打印奇数 //部分结 ...
- 3、JS中的语句
1.块语句 常用于组合0~多个语句:语法:{语句1;语句2……} *没有块级作用域:如:在for循环里面定义一个自增数 i,可以在for循环外取到它跳出循环时的值,而且for循环里面新定义的数据也能取 ...
- [转]设置Android手机以使用ARM Streamline进行性能分析(二)
原文因为arm社区改版访问不到了,原作者鲍方,原文地址,这篇是从google cache里挖出来的,希望能帮到要对cocos2dx优化的各位 Posted by Fang Bao, Leave C ...
- Sprint第二个冲刺(第十一天)
看板: 燃尽图:
- HDU 2277 Change the ball
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277 Change the ball Time Limit: 2000/1000 MS (Java/O ...
- Timer和TimerTask的用法
最近在做java课程设计的时候,我用到了timer,于是学习了一下timer的用法. java实现多线程比较常用的两种方法,一种是直接继承Thread类,另一种则是实现Runnable接口.Timer ...