判断单链表是否有环 (leetcode 141)

一:解题思路

这道题目有2种解题方法。

第一种:循环遍历单链表中的每个节点,将每个节点加入一个集合set中去,如果遍历的节点在集合set中出现过,这说明这个单链表一定有环。Time:O(n),Space:O(n)

第二种:用快慢指针法来解决这个问题。定义快指针和慢指针都指向链表的头部,快指针每次走2步,慢指针每次走1步,如果链表有环,进入环内。这就变成了一个追击问题,我们知道快指针一定能够追上慢指针。Time:O(n),Space:O(1)

二:完整代码示例 (C、C++、Java、Python)

方法一C:

方法二C:

bool hasCycle(struct ListNode *head) 
{
    if (head == NULL || head->next == NULL) return false;
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    
    while (fast != NULL && fast->next != NULL)
    {
        fast = fast->next->next;
        slow = slow->next;

        if (fast == slow) return true;
    }

    return false;
}

方法一C++:

class Solution {
public:
    bool hasCycle(ListNode *head) 
    {
        if (head == NULL || head->next == NULL) return false;

        set<ListNode*> s;
        for (ListNode* p = head; p != NULL; p = p->next)
        {
            if (s.count(p) > 0) return true;
            s.insert(p);
        }

        return false;
    }
};

方法二C++:

class Solution {
public:
    bool hasCycle(ListNode *head) 
    {
        if (head == NULL || head->next == NULL) return false;
        
        ListNode* slow = head;
        ListNode* fast = head;

        while (fast != NULL && fast->next != NULL)
        {
            slow = slow->next;
            fast = fast->next->next;

            if (fast == slow) return true;
        }

        return false;
    }
};

方法一Java:

public class Solution {
        public boolean hasCycle(ListNode head) {
                if(head==null || head.next==null) return false;
                Set<ListNode> s=new HashSet<>();
                
                for(ListNode p=head;p!=null;p=p.next)
                {
                    if(s.contains(p)) return true;
                    s.add(p);
                }
                
                return false;
        }
    }

方法二Java:

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;
        }
        }

方法一Python:

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if head==None: return False
        if head.next==None: return False
        s={}
        while head:
            if s.get(head):
                return True
            s[head]=1
            head=head.next
        return False

方法二Python:

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if head==None: return False
        if head.next==None: return False
        
        slow,fast=head,head
        while fast and fast.next:
            slow=slow.next
            fast=fast.next.next
            if slow==fast:
                return True
        return False
posted @ 2020-03-12 15:18  repinkply  阅读(246)  评论(0)    收藏  举报