Linked List Cycle
来源:互联网 发布:高性能网络编程 二 编辑:程序博客网 时间:2024/06/05 18:07
/* * Solution.cpp * * Created on: 2014年4月9日 * Author: William */#include <iostream>using namespace std;// 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 == NULL)return false;ListNode *fast = head;ListNode *slow = NULL;// See below:while (fast != NULL && fast != slow) {if (slow == NULL)// We can use do..while.. to avoid this comparison trick.slow = head;fast = fast->next;if (fast != NULL) {fast = fast->next;slow = slow->next;}}if (fast == slow)return true;elsereturn false;}// The following function is not usable because it will damage the original linked list.// It's not a check function!!!!//bool hasCycle(ListNode *head) {//ListNode *p = head;//while (p != NULL) {//if (p->next == head)//return true;//ListNode *q = p;//// The order of next two lines is important.//p = p->next;//q->next = head;//}//return false;//}};
0 0
- leetcode Linked List Cycle & Linked List Cycle ||
- Linked List Cycle II Linked List Cycle
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle II
- [LeetCode]Linked List Cycle
- Linked List Cycle
- LeetCode | Linked List Cycle
- 阳光梦:nginx上搭建HLS流媒体服务器 (4)-参考
- Linked List Cycle II
- lua程序设计 28.2 Metatables userdata程序范例
- InfoPath的未来
- Visual C# 字体对话框 & 颜色对话框
- Linked List Cycle
- 新浪&腾讯微博:MySQL数据库主表设计猜想
- spring学习笔记(6)--自动装配autowire
- 逆波兰表达式运算
- socket 非阻塞模式下的connect封装实现
- CSDN编程挑战之彩色石子(使用暴力破解法)
- Java排序算法优化--插入排序【温故而知新】
- 线程安全之Collections.synchronizedList
- android开发笔记之也谈android开发入门