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?

 1 /*************************************************************************
 2     > File Name: LeetCode141.c
 3     > Author: Juntaran
 4     > Mail: JuntaranMail@gmail.com
 5     > Created Time: Mon 16 May 2016 18:45:35 PM CST
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9     
10     Linked List Cycle
11     
12     Given a linked list, determine if it has a cycle in it.
13 
14     Follow up:
15     Can you solve it without using extra space?
16 
17  ************************************************************************/
18 
19 #include <stdio.h>
20 
21 /**
22  * Definition for singly-linked list.
23  * struct ListNode {
24  *     int val;
25  *     struct ListNode *next;
26  * };
27  */
28 int hasCycle(struct ListNode *head)
29 {
30 
31     struct ListNode *fast = head;
32     struct ListNode *slow = head;
33 
34     while( slow && fast && fast->next )
35     {
36         fast = fast->next->next;
37         slow = slow->next;
38 
39         if( fast == slow )
40         {
41             return 1;
42         }
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/Juntaran/p/5511679.html