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 条评论
登录 后参与评论

相关文章

来自专栏desperate633

LintCode 翻转链表题目分析代码

样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null

673
来自专栏Jack-Cui

160. Intersection of Two Linked Lists(Linked List-Easy)

    Write a program to find the node at which the intersection of two singly lin...

1805
来自专栏开发 & 算法杂谈

PAT Advanced 1043

A Binary Search Tree (BST) is recursively defined as a binary tree which has th...

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

Leetcode 92 Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. Fo...

1588
来自专栏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...

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

Leetcode 148 SortList

Sort a linked list in O(n log n) time using constant space complexity. 链表排序,n...

1817
来自专栏aCloudDeveloper

LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy

题目:Merge Two Sorted Lists Merge two sorted linked lists and return it as a new l...

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

Leetcode 2 Add Two Numbers

题目没有说清楚,trick挺多,考察细心程度的水题吧。 You are given two linked lists representing two ...

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

Leetcode 86 Partition List

Given a linked list and a value x, partition it such that all nodes less than x...

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

Leetcode 21 Merge Two Sorted Lists

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

1909

扫码关注云+社区