leetCode-linkedListCycle判断链表是否有环
来源:互联网 发布:怎样开农村淘宝服务站 编辑:程序博客网 时间:2024/06/06 14:20
题目
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
分析
判断链表是否有环,采用快慢指针,如果相遇则表示有环
AC代码
/** * 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 || !head->next){ return false; } ListNode* slow = head; ListNode* fast = head->next; while(fast->next && fast->next->next && fast != slow){ fast = fast->next->next; slow = slow->next; } return fast == slow; }};
阅读全文
0 0
- leetCode-linkedListCycle判断链表是否有环
- LeetCode -- LinkedListCycle
- leetcode--Linked List Cycle--判断链表是否有环
- 判断链表是否有环---leetcode题目
- LeetCode钻研9 判断链表是否有环
- LeetCode基础-链表-判断链表是否有环
- leetcode OJ 判断单链表中是否有环
- LeetCode -- 判断链表中是否有环
- 判断链表是否有环
- 判断链表是否有环
- 如何判断链表是否有环
- 判断链表是否有环
- 判断链表是否有环
- 判断一个链表是否有环
- 单向链表判断是否有环
- 判断链表是否有环
- 判断一个链表是否有环
- 判断链表是否有环
- 【单调栈思路】Minimal Subarray Length UVALive
- 四种常见的 POST 提交数据方式
- Python学习笔记——函数
- stm32 初学笔记(寄存器)--感悟
- 时间类数字类枚举类例题·
- leetCode-linkedListCycle判断链表是否有环
- 1003
- 当导入项目时候报错/Error:Jack is required to support java 8 language features.
- Java进阶(四十)Java类、变量、方法修饰符讲解 Java类修饰符
- go中的main函数和init函数
- 2017 Multi-University Training Contest
- 走进Vue.js
- MySQL分区的管理
- CentOS配置时钟同步服务器