141. Linked List Cycle(Linked List-Easy)

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

Follow up:     Can you solve it without using extra space?

题目:判断一个给定的列表,是否成环,也就是,判断是否首尾相连。

思路:创建一个快指针,一个慢指针。快指针一次走两布,慢指针一次走一步。慢指针走一圈,快指针走两圈,如果两个指针指向位置相同了,说明列表是环状的。

Language:c

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

Language : cpp

/**
 * 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 *fast = head;
        ListNode *slow = head;
        while(fast != NULL && fast->next != NULL){
            slow = slow->next;
            fast = fast->next->next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
};

Language:python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        try:
            slow = head
            fast = head.next
            while slow is not fast:
                slow = slow.next
                fast = fast.next.next
            return True
        except:
            return False

LeetCode题目汇总: https://github.com/Jack-Cherish/LeetCode

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Jack-Cui

21. Merge Two Sorted Lists(Linked List-Easy)

Merge two sorted linked lists and return it as a new list. The new list should b...

18010
来自专栏小樱的经验随笔

Codeforces Round #300(A.【字符串,多方法】,B.【思维题】,C.【贪心,数学】)

A. Cutting Banner time limit per test:2 seconds memory limit per test:256 megaby...

28712
来自专栏尾尾部落

[剑指offer] 合并两个排序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

683
来自专栏Jack-Cui

234. Palindrome Linked List(Linked List-Easy)

Given a singly linked list, determine if it is a palindrome. Follow up: Could yo...

18810
来自专栏计算机视觉与深度学习基础

Leetcode 141 Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you so...

1789
来自专栏Jack-Cui

83. Remove Duplicates from Sorted List(Linked List-Easy)

Given a sorted linked list, delete all duplicates such that each element appear ...

1767
来自专栏Jack-Cui

206. Reverse Linked List(Linked List-Easy)

Reverse a singly linked list. Hint: A linked list can be reversed either iterati...

1806
来自专栏desperate633

LeetCode 24. Swap Nodes in Pairs题目分析代码

561
来自专栏ml

HDUOJ-----(1251)统计难题

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Jav...

3509
来自专栏HansBug's Lab

1054: [HAOI2008]移动玩具

1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1272  Solv...

2376

扫码关注云+社区