码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode141-环形链表

时间:2018-12-01 13:18:58      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:false   lin   判断   style   etc   空间   tco   使用   def   

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode *slow, *fast;
    slow = fast = head;
    while(slow != NULL && fast != NULL && fast->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast)
            return true;
    }
    return false;
}

给定一个链表,判断链表中是否有环。

进阶:
你能否不使用额外空间解决此题?

leetcode141-环形链表

标签:false   lin   判断   style   etc   空间   tco   使用   def   

原文地址:https://www.cnblogs.com/xinfenglee/p/10048758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!