首页 > 其他 > 详细

LeetCode:141 环形链表(哈希表)

时间:2020-10-02 09:43:54      阅读:27      评论:0      收藏:0      [点我收藏+]
/**
 * 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) {
        Set<ListNode> s = new HashSet<>();
    
        while(head!=null)
        {
            if(s.contains(head)){
                return true;
            }
            else{
                s.add(head);
                head = head.next;
            }
        }

        return false;
    }
}

 

LeetCode:141 环形链表(哈希表)

原文:https://www.cnblogs.com/dloooooo/p/13760273.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!