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 ...
随机推荐
- 14.7.1&14.7.2
ArrayList <Object> list = new ArrayList<>(); //实例化ArrayList int i; int j; for(i = 1; i & ...
- 转)MySQL日期与时间函数
-- MySQL日期时间处理函数 -- 当前日期:2017-05-12(突然发现今天512,是不是会拉防空警报) SELECT NOW() FROM DUAL;-- 当前日期时间:2017-05-12 ...
- 转)安装svn服务器
以下转载自:http://www.linuxidc.com/Linux/2015-01/111956.htm 安装 安装软件包: sudo apt-get install subversion 配置 ...
- OrgChart 组织架构与PHP结合使用
一.OrgChart下载地址: https://github.com/dabeng/OrgChart 二.组织架构的表设计 CREATE TABLE `org_info` ( `id` int(11) ...
- phpcms如何给已有的模块添加新功能?
phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...
- ecplice中代码使用快捷键无法格式化,使用其他方法将代码格式化的步骤
选中需要进行格式化的代码--->右键--->source--->format,就可以将代码格式化了.
- AspectJ的XML方式完成AOP的开发之AOP的通知类型
1. 前置通知 * 在目标类的方法执行之前执行. * 配置文件信息:<aop:after method="before" pointcut-ref="myPoint ...
- win, cmd下安装mysql(win真tm难用)
常用命令: 修改root用户密码 update mysql.user set authentication_string=password('1234qwer') where user='root' ...
- [freeCodeCamp] solution to HTTP JSON API SERVER passed!
var http = require('http') var url = require('url') function parsetime (time) { return { hour: time. ...
- 开发中常遇到的Python陷阱和注意点-乾颐堂
最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...