LeetCode刷题(C++)——Linked List Cycle(Easy)
来源:互联网 发布:我国的医疗数据网站 编辑:程序博客网 时间:2024/05/29 12:28
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
思路:设置两个指针fast和slow,fast一次走两步,slow一次走一步,如果链表有环,两指针一定相遇,并且一定是在环中相遇。否则,链表就没有环
/** * 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->next;ListNode* slow = head;while (fast != NULL) {if (fast == slow) {return true;}slow = slow->next;fast = fast->next;if (fast != NULL)fast = fast->next;}return false; }};
阅读全文
1 0
- LeetCode刷题(C++)——Linked List Cycle(Easy)
- LeetCode-Easy刷题(32) Linked List Cycle
- leetcode算法题easy心得(一)Linked List Cycle
- LeetCode刷题(C++)——Linked List Cycle II(Medium)
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- Leetcode 141. Linked List Cycle (Easy) (cpp)
- 【Leetcode-Easy-141】Linked List Cycle
- 【leetcode】Linked List —— easy(java)
- LeetCode——Linked List Cycle
- LeetCode——Linked List Cycle II
- LeetCode——Linked List Cycle
- Linked List Cycle —— Leetcode
- LeetCode——Linked List Cycle II
- leetcode 141 —— Linked List Cycle
- LeetCode题解——Linked List Cycle
- LeetCode——141. Linked List Cycle
- LeetCode——Linked List Cycle
- LeetCode — Linked List Cycle 解题报告
- 实现一个简单的类似 TabLayout 的顶部指示器
- 【区块链学习】以Windows为例,通过以太坊钱包部署和运行智能合约
- juqery ajax 请求时传递数组参数
- 登陆注册案例——IO流实现
- xcode调试技巧大全--exception breakpoint, symbolic breakpoint
- LeetCode刷题(C++)——Linked List Cycle(Easy)
- CRC16检验
- 函数对象
- 【编程题】字符串分类
- mac 下 激活 phpstorm 装php
- CF
- 【开班啦】机器学习精英40天成长计划
- Java单例模式和关键字
- JavaScript DOM编程艺术第十章