141. Linked List Cycle
来源:互联网 发布:音乐剪辑软件手机 编辑:程序博客网 时间:2024/05/21 03:24
Given a linked list, determine if it has a cycle in it.
solution :
/** * 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 *fast = head; ListNode *slow = head; while(fast&&fast->next){ fast = fast->next->next; slow = slow->next; if(fast == slow) return true; } return false; }};
心得:快慢指针法,要想到这个就比较简单。
运行速度:快
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- php基础之转码
- TOMCAT 调优学习
- H5与iOS交互
- Python爬虫开发(一):零基础入门
- SAS Hierarchy Cluster
- 141. Linked List Cycle
- 算法基础 2.1插入排序
- 海量数据相似度计算之simhash和海明距离
- 299. Bulls and Cows
- SAS - KNN Cluster
- 知识积累:JAVA的引用传递和值传递
- Oracle数据库-实用SQL
- JAVA中使用TCP协议编程模拟C/S模式
- 【leetcode】【29】Divide Two Integers