Problem Linked List Cycle

Problem Description:

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

 Solution:

 1  public boolean hasCycle(ListNode head) {
 2         if (head == null) return false;
 3         if (head.next == null || head.next.next == null) return false;
 4         ListNode tortoise = head.next;
 5         ListNode hare = head.next.next;
 6         while (tortoise != hare) {
 7             if (tortoise.next == null) return false;
 8             tortoise = tortoise.next;
 9             if (hare.next == null || hare.next.next == null) return false;
10             hare = hare.next.next;
11         }
12
13         return true;
14     }

Problem Linked List Cycle

时间: 2024-11-08 21:35:16

Problem Linked List Cycle的相关文章

Problem Linked List Cycle II

Problem Description: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space?  Solution: 1 public ListNode detectCycle(ListNode head) { 2 if (head == null) r

[LintCode] Linked List Cycle(带环链表)

描述 给定一个链表,判断它是否有环. 样例 给出 -21->10->4->5, tail connects to node index 1,返回 true. 这里解释下,题目的意思,在英文原题中,tail connects to node index 1 表示的是节点 5 还要链接回索引号 为 1 的节点. 一个典型的带环链表如下: 挑战 不要使用额外的空间 代码 GitHub 的源代码,请访问下面的链接: https://github.com/cwiki-us/java-tutoria

[LeetCode]Linked List Cycle

题目:Linked List Cycle 判断一个单链表中是否有环,要求常量空间复杂度: 思路: 使用两个指针同时从链表表头开始移动,一个移动一步,一个移动两步,直到两个指针重合或某一指针指向链尾. 两个指针重合则单链表有环存在,否则没有. 第二个指针以第一个指针的两倍的速度移动,而第一个指针每次移动一步,这样只要有环,两个指针必定能重合. bool LeetCode::hasCycle(ListNode *head){ if (!head)return false; ListNode *p =

LeetCode(141): Linked List Cycle

Linked List Cycle: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. c++ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool ha

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

【LeetCode】Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? (Easy) 分析: 采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环. 代码: 1 /** 2 * Definition for singly-linked list.