首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >LeetCode 0141 - Linked List Cycle

LeetCode 0141 - Linked List Cycle

作者头像
Reck Zhang
发布2021-08-11 14:47:17
发布2021-08-11 14:47:17
4050
举报
文章被收录于专栏:Reck ZhangReck Zhang

Linked List Cycle

Desicription

Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

Solution

代码语言:javascript
复制
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        while(head) {
            if(head->val == INT_MIN)
                return true;
            head->val = INT_MIN;
            head = head->next;
        }
        return false;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • Linked List Cycle
    • Desicription
    • Solution
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档