【LeetCode】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?

Hide Tags: Linked List Two Pointers

Hide Similar Problems: (M) Linked List Cycle II

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 *faster = head;
        ListNode *slower = head;
        while(faster->next && faster->next->next){
            faster=faster->next->next;
            slower=slower->next;
            if(faster==slower)
                return true;
        }
        return false;
    }
};
时间: 2024-10-08 04:21:45

【LeetCode】141 - Linked List Cycle的相关文章

【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? Solution: Discuss上的分析:Suppose the first meet at step k,the length of the Cycle is r. so..2k-k=nr,k=n

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

<LeetCode OJ> 141. Linked List Cycle

141. Linked List Cycle My Submissions Question Total Accepted: 88665 Total Submissions: 241622 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Subscribe to see which co

【LeetCode】Reverse Linked List II 解题报告

[题目] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

【LeetCode】2.Linked List — Linked List Cycle II 链表环2

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to.

[Leetcode]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? 让head不断走一步,如果cur不为null,走两步,这样如果存在cycle,cur会追上head. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *

【Lintcode】102.Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, tail connects to node index 1, return true 题解: Solution 1 () class Solution { public: bool hasCycle(ListNode *head) { if (!head) { return false; } ListNode*

【Leetcode】Reverse Linked List

原题链接:https://leetcode.com/problems/reverse-linked-list/ 题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: 头插法建立链表 算法: [java] view plain copy public ListNode reverseL