[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?

让head不断走一步,如果cur不为null,走两步,这样如果存在cycle,cur会追上head。

/**
 * 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) {
        ListNode* cur = head;

        while (cur && cur->next != head){
            cur = cur->next;
            if (cur)
                cur = cur->next;
            else
                break;
            head = head->next;
        }
        if (!cur) return false;
        return true;
    }
};
时间: 2024-10-16 22:16:29

[Leetcode]141. Linked List Cycle的相关文章

<LeetCode OJ> 141. Linked List Cycle

141. Linked List Cycle My Submissions Question Total Accepted: 88665 Total Submissions: 241622 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Subscribe to see which co

【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? Hide Tags: Linked List Two Pointers Hide Similar Problems: (M) Linked List Cycle II Solution: /** * Definition for singly-linked list. * str

leetCode 141. Linked List Cycle 链表

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? 题目大意: 判断一个单链表是否存在环. 思路: 采用快慢指针来处理. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int va

Leetcode 线性表 Linked List Cycle

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle Total Accepted: 17041 Total Submissions: 48975 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否有环 思路:快慢

LeetCode解题报告:Linked List Cycle && Linked List Cycle II

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? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

141. Linked List Cycle && 142. Linked List Cycle II

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { pu

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

LeetCode(141): Linked List Cycle

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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

leetcode:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50412899 题目地址:https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return