带环链表-LintCode
来源:互联网 发布:象牙社区一样的软件 编辑:程序博客网 时间:2024/04/29 14:43
给定一个链表,判断它是否有环。
思想:
设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇。
#ifndef C102_H#define C102_H#include<iostream>using namespace std;class ListNode{public: int val; ListNode *next; ListNode(int val){ this->val = val; this->next = NULL; }};class Solution {public: /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ bool hasCycle(ListNode *head) { // write your code here ListNode *fast = head, *slow = head; while (fast&&fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) { return true; break; } } return false; }};#endif
阅读全文
0 0
- lintcode-带环链表
- lintcode,带环链表
- 带环链表-LintCode
- [Lintcode]带环链表
- lintcode带环链表
- LintCode 带环链表
- LintCode-带环链表 II
- lintcode-带环链表-102
- LintCode: 带环链表 II
- *[Lintcode]带环链表 II
- lintcode(102)带环链表
- 带环链表 II-LintCode
- [LintCode]102.带环链表
- lintcode--带环链表II
- LintCode:M-带环链表
- 【LintCode】 Linked List Cycle 带环链表
- LintCode(102) 带环链表
- LintCode(103)带环链表 II
- 开发利器总结(一)
- NodeJS通过ffi调用DLL
- 武将属性基础模型
- 组合数算法,紫书P64
- vue组件中camelCased (驼峰式) 命名与 kebab-case(短横线命名)
- 带环链表-LintCode
- java.util.concurrent包下的几个常用类
- Javascript 判断变量类型的陷阱 与 正确的处理方式
- C#正则表达式(二)
- 获得dll,exe中图标资源总个数
- 一篇就够了系列之Android Manifest全解析
- GitLab新建项目
- 欢迎使用CSDN-markdown编辑器
- Android 自定义View实战系列 :时间轴