leetcode 141: Linked List Cycle
2015-08-19 14:20
495 查看
Traversal the Linked List, and at the mean time, reverse the direction. If the traversal finally reaches head again, there exists a cycle.
/**
* 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)
return false;
ListNode *curr=head->next,*pre=head;
while(curr)
{
if(curr==head)
return true;
ListNode *temp=curr->next;
curr->next=pre;
pre=curr;
curr=temp;
}
return false;
}
};
/**
* 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)
return false;
ListNode *curr=head->next,*pre=head;
while(curr)
{
if(curr==head)
return true;
ListNode *temp=curr->next;
curr->next=pre;
pre=curr;
curr=temp;
}
return false;
}
};
相关文章推荐
- c++ primer plus 第九章《编程题9.6.2》
- Java 解決BufferedReader读取UTF-8文件中文乱码
- 九度OJ 题目1059:abc
- Android设计模式系列-适配器模式
- SO_REUSEADDR说明
- Oracle数据字典详解
- Oracle Query Performance Tuning: 12–Step Program
- 应用程序中嵌入的网页运行时出现“脚本错误”解决方法
- Java-String 字符串
- springmvc和encache集成关键代码和总结
- EasyUI的验证框
- hdu1035
- mysql数据类型介绍(含text,longtext,mediumtext说明)
- tomcat7下启动jmx服务
- Android emulator error: x86 emulation currently requires hardware acceleration的解决方案
- windows系统原版下载
- Java-按值传递还是按引用传递详细解说
- 公钥加密算法到底什么鬼
- 九度OJ 题目1058:反序输出
- Android设计模式系列--工厂方法模式