• 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 }
  • 相关阅读:
    Linux中应用程序如何使用系统调用syscall
    Spring Service、Dao进行Junit单元测试
    spring service层单元测试
    java-随机生成用户名(中文版及英文版)
    Java获取随机数
    JUnit单元测试框架的使用
    Mock以及Mockito的使用
    Spring Boot 各Starter介绍
    编写自己的SpringBoot-starter
    Spring Boot使用单元测试
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511680.html
Copyright © 2020-2023  润新知