LeetCode141: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?
解题思路:
判断链表有无环,可用快慢指针进行,快指针每次走两步,慢指针每次走一步,如果快指针追上了慢指针,则存在环,否则,快指针走到链表末尾即为NULL是也没追上,则无环。
为什么快慢指针可以判断有无环?
因为快指针先进入环,在慢指针进入之后,如果把慢指针看作在前面,快指针在后面每次循环都向慢指针靠近1,所以一定会相遇,而不会出现快指针直接跳过慢指针的情况。
实现代码:
#include <iostream>
using namespace std; /**
Linked List Cycle
*/ struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void addNode(ListNode* &head, int val)
{
ListNode *node = new ListNode(val);
if(head == NULL)
{
head = node;
}
else
{
node->next = head;
head = node;
}
}
void printList(ListNode *head)
{
while(head)
{
cout<<head->val<<" ";
head = head->next;
}
} class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL || head->next == NULL)
return NULL;
ListNode *quick = head;
ListNode *slow = head;
while(quick && quick->next)//利用快慢指针判断有无环
{
quick = quick->next->next;
slow = slow->next;
if(quick == slow)
return true;
}
return NULL; }
};
int main(void)
{
ListNode *head = new ListNode();
ListNode *node1 = new ListNode();
ListNode *node2 = new ListNode();
ListNode *node3 = new ListNode();
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = node1; Solution solution;
bool ret = solution.hasCycle(head);
if(ret)
cout<<"has cycle"<<endl; return ;
}
LeetCode141:Linked List Cycle的更多相关文章
- 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 ...
- LeetCode142:Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode OJ: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, 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] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
随机推荐
- Dice
Dice http://acm.hdu.edu.cn/showproblem.php?pid=5012 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Python错误:close failed in file object destructor
我遇到的情况: 二进制程序调shell再调Python后,shell退出,Python进程挂到init上(不是僵尸进程),但 此时二进制程序未退出,这时候中断而二进制程序出现此提示. 经查询: 应该是 ...
- python之socket运用之执行命令
服务端的代码 import socket import subprocess HOST = "127.0.0.1" PORT = 5001 ip_bind = (HOST,PORT ...
- fusioncharts 破解方法(转载)
FusionCharts是一个Flash的图表组件,它可以用来制作数据动画图表,其中动画效果用的是Adobe Flash 8 (原Macromedia Flash的)制作的flash , Fusion ...
- 清空表中数据 id从1开始
删除表的记录以后,如何使新记录的编号仍然从1开始有两种方法: 方法1: truncate table 你的表名 --这样不但将数据删除,而且可以重新置位identity属性的字段. 方法2: dele ...
- mRemoteNG
mRemoteNG 1.摆脱了mstsc那种一个程序一个界面的模式,采用了左边树+右边Tab页的显示形式,让你在一个mRemote界面中,可以连接多个远程桌面,再也不用为切来切去而烦恼了(如上图). ...
- Stripies
/* Our chemical biologists have invented a new very useful form of life called stripies (in fact, th ...
- jstl标签详解 (转载)
JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实. JSTL ...
- [BAT]批处理脚本双击可运行,但在定时计划任务中无法执行(当前工作路径不对导致的)
一开始,红色部分我是用set AutoPath=%cd%,双击可执行,但是将这个批处理脚本放在定时任务中无法执行,后来发现在定时执行的时候,当前工作路径不是批处理脚本所在的路径,而是C:/Window ...
- 慢工出细活,Facebook点赞按钮设计中的门道
一年前,Facebook点赞按钮发布更新.一年后的今天,Facebook小小的点赞按钮因为Ted刚发布的一段演讲掀起波澜.设计一个像FB点赞按钮那么小的东西很难么?Ted中Margaret Gould ...