zoukankan      html  css  js  c++  java
  • LeetCode 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.

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

     1 /*************************************************************************
     2     > File Name: LeetCode142.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Mon 16 May 2016 19:46:12 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Linked List Cycle II
    11     
    12     Given a linked list, return the node where the cycle begins. 
    13     If there is no cycle, return null.
    14 
    15     Note: Do not modify the linked list.
    16 
    17     Follow up:
    18     Can you solve it without using extra space?
    19 
    20  ************************************************************************/
    21 
    22 #include <stdio.h>
    23 
    24 /**
    25  * Definition for singly-linked list.
    26  * struct ListNode {
    27  *     int val;
    28  *     struct ListNode *next;
    29  * };
    30  */
    31 struct ListNode *detectCycle(struct ListNode *head)
    32 {
    33 
    34     struct ListNode *fast = head;
    35     struct ListNode *slow = head;
    36 
    37     int count1 = 0;
    38     int count2 = 0;
    39     while( slow && fast && fast->next )
    40     {
    41         fast = fast->next->next;
    42         slow = slow->next;
    43 
    44         if( slow == fast )
    45         {
    46             slow = head;
    47             while( slow != fast )
    48             {
    49                 slow = slow->next;
    50                 fast = fast->next;
    51             }
    52             return slow;
    53         }
    54     }
    55     return NULL;
    56 }
  • 相关阅读:
    Zset-ZREVRANGEBYSCORE
    Zset-ZREVERANGE
    Zset-ZRANGEBYSCORE
    Leetcode1550. 存在连续三个奇数的数组
    Java中的IO流
    线程间通信(也叫线程并发协作)的四种方式
    数据库三大范式
    MVCC(Multi-Version Concurrency Control):多版本并发控制详解
    Java三种单例模式实现
    Java的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511680.html
Copyright © 2011-2022 走看看