141. Linked List Cycle && 142. Linked List Cycle II

141. Linked List Cycle

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

/**
 * 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) {
        if(head == null || head.next == null)
            return false;

        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)
                return true;
        }
        return false;
    }
}

142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null)
            return null;

        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)
                break;
        }
        if(fast.next == null || fast.next.next == null)
            return null;

        ListNode l = head;
        while(l != slow)
        {
            l = l.next;
            slow = slow.next;
        }
        return l;
    }
}
时间: 2024-10-11 12:24:42

141. Linked List Cycle && 142. Linked List Cycle II的相关文章

刷题142. Linked List Cycle II

一.题目说明 题目142. Linked List Cycle II,判断一个链表是否有环,如果有返回环的第一个元素,否则返回NULL. 这个题目是141. Linked List Cycle的升级版本,难度是Medium! 二.我的解答 最直观的解答就是用一个unordered_map<ListNode*,int> dp来统计节点出现的次数,如果出现2,则这个就是第一个节点. class Solution{ public: ListNode* detectCycle(ListNode* he

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.

LeetCode 141, 142. Linked List Cycle I+II

判断链表有没有环,用Floyd Cycle Detection算法,用两个快慢指针. class Solution { public: bool hasCycle(ListNode *head) { if (!head) return false; ListNode *slow, *fast; slow=fast=head; do{ if (fast==NULL || fast->next==NULL) return false; slow = slow->next; fast = fast-

leetcode 141 142. Linked List Cycle

题目描述: 不用辅助空间判断,链表中是否有环 /** * 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 ==

Leetcode 142 Linked List Cycle II

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? 同Leetcode 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

leetcode 142. Linked List Cycle II ----- java

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 141题的延伸,求出循环点. 可以用数学方法证明出slow与find相遇的位置一定是所求的点. /** * Definitio

[leedcode 142] Linked List Cycle II

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? 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!).  我们已经得到了结论a=c,那么让两个指针分别从X

Java for LeetCode 142 Linked List Cycle II

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? 解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II JAVA实现如下: public ListNode detectCycle(Li

Leetcode 142. Linked List Cycle IIJAVA语言

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 题意:不破坏原链表的情况下判断有没有环,,,,,, /**  * Definition for singly-linked list.  * class ListNode {  *     int val;  *     ListNo