Algorithms—141.Linked List Cycle
2015-07-16 17:57
781 查看
思路:经典跑圈。
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while (true) {
if (fast==null||fast.next==null||slow==null) {
return false;
}
fast=fast.next.next;
slow=slow.next;
if (fast==slow) {
return true;
}
}
}
}
耗时:348ms,中游
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while (true) {
if (fast==null||fast.next==null||slow==null) {
return false;
}
fast=fast.next.next;
slow=slow.next;
if (fast==slow) {
return true;
}
}
}
}
耗时:348ms,中游
相关文章推荐
- Hard to say good bye
- EGOImageView的使用方法及注意事项
- ModelGoon安装
- Django中模版的子目录与include标签的使用方法
- 系统进程 zygote(一)—— 概述
- Algorithm --> 快速排序
- stl之基本算法《stl_algobase.h》
- Google推荐的图片加载库Glide介绍(转)
- 阿里将成为下一个谷歌?是谁Google真正的挑战者
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- django时间查询
- Algorithms—108.Convert Sorted Array to Binary Search Tree
- Algorithms—106.Construct Binary Tree from Inorder and Postorder Traversal
- 设置快速访问google的dns
- Algorithms—105.Construct Binary Tree from Preorder and Inorder Traversal
- 开机logo切换逻辑深入研究
- 开机logo切换逻辑深入研究
- Algorithms—107.Binary Tree Level Order Traversal II
- Algorithms—104.Maximum Depth of Binary Tree
- Algorithms—111.Minimum Depth of Binary Tree