LeetCode 141

Posted Juntaran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: [email protected]
 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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

LeetCode第141题—环形链表—Python实现

LeetCode 141. 环形链表

关于在各浏览器中插入音频文件的html代码片段

p141 二叉树的 Z 字形遍历(leetcode 103)

[leetcode]141. Linked List Cycle判断链表是否有环