首页 > 其他 > 详细

LeetCode 141

时间:2016-05-20 13:12:23      阅读:118      评论:0      收藏:0      [点我收藏+]

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 }

 

LeetCode 141

原文:http://www.cnblogs.com/Juntaran/p/5511679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!