【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* fast = head;
        ListNode* slow = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) {
                return true;
            }
        }

        return false;
    }
};
时间: 2024-10-13 16:05:48

【Lintcode】102.Linked List Cycle的相关文章

【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. * str

【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】链表 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 =

【DataStructure】 Classical Question: Josephus Cycle

[Description] This problem is based upon a report by the historian Joseph ben Matthias (Josephus) on the outcome of a suicide pact that he had made between himself and 40 soldiers as they were besieged by superior Roman forces in 67 A.D. Josephus pro

【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

【HDOJ】3560 Graph’s Cycle Component

并查集的路径压缩. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 100005 5 6 int deg[MAXNUM], bin[MAXNUM]; 7 char isCycle[MAXNUM]; 8 9 int find(int x) { 10 int r = x; 11 int i = x, j; 12 13 while (r != bin[r]) 14 r = bin[r]; 15 16 while

【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

【C#】#102 发送邮件

项目需求:定时的发送邮件,于是学习了如何发送邮件 下面有一个简单的例子.能够实现简单的发送邮件,加上附件可以添加一个属性[Attachment],然后配置上附件的路径 Demo下载 代码总共只有一下这一些,就可以发送邮件. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace SendMail { public c

【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.