LeetCode141. 环形链表🌟🌟🌟🌟🌟简单

课后作业

问题描述

原文链接:141. 环形链表

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

如果链表中存在环 ,则返回 true 。 否则,返回 false 。

示例 1:

img

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

img

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

img

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。

提示:

  • 链表中节点的数目范围是 [0, 104]
  • -105 <= Node.val <= 105
  • pos 为 -1 或者链表中的一个有效索引 。

代码实现

Java

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;

    }
}

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
        """
        if not head:
            return False

        fast = head
        slow = head

        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

            if fast == slow:
                return True

        return False

C++

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

        while(fast && fast->next){
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
};

Go

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func hasCycle(head *ListNode) bool {
    if head == nil {
        return false
    }

    fast := head
    slow := head

    for fast != nil && fast.Next != nil {
        fast = fast.Next.Next
        slow = slow.Next

        if fast == slow {
            return true
        }
    }

    return false
}

发表回复

后才能评论

评论(5)

  • Yuki 永久会员 2024年1月30日 下午4:09

    打卡
    class Solution {
    public:
    bool hasCycle(ListNode *head) {
    if(headNULL||head->nextNULL)
    return false;

        ListNode*fast=head;
        ListNode*slow=head;
    
        while(fast!=NULL&&fast->next!=NULL){
            //没走到出口
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast){//相遇说明有环
                return true;
            }
        }
    
        return false;
    
    }
    

    };

  • uu 永久会员 2023年9月20日 下午4:03
    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    
     // 【思路】用快慢指针,想到了追及问题,最多快的跑多一圈
     // 时间复杂度:O(2n)->O(n)
     // 空间复杂度:O(1)
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head == null || head.next == null) {
                return false;
            }
            ListNode slow = head;
            ListNode fast = head;
    
    
            while(fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if(fast == slow) {
                    return true;
                }
            }
            return false;
        }
    }
    
  • 泡芙 永久会员 2023年9月8日 下午3:23

    快慢指针:当快慢指针相遇时,说明链表有环

    • Time:O(n)
    • Space:O(1)
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if (head == null || head.next == null) {
                return false;
            }
    
            ListNode fast = head;
            ListNode slow = head;
    
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (fast == slow) {
                    return true;
                }
            }
            return false;
        }
    }
    
  • 阿尚 普通 2023年8月18日 下午1:49
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head == null) return false;
            //快慢指针:若链表有环就会相遇
            ListNode fast = head.next;
            ListNode slow = head;
            while(fast != null && fast.next != null){
                if(slow == fast) return true;
                slow = slow.next;
                fast = fast.next.next;
            }
    
            return false;
        }
    }
    
  • 帅气的冬瓜 普通 2023年6月28日 下午4:46

    打卡

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理