题解 | #判断链表中是否有环#

判断链表中是否有环

http://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9


/**
 * 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 head0 = head;
        HashSet<ListNode> hashSet = new HashSet<>();
        while(head0 != null){
            if(hashSet.contains(head0)){
                return true;
            }else{
                hashSet.add(head0);
            }
            head0 = head0.next;
        }
        
        return false;
    }
}
全部评论

相关推荐

02-11 14:29
已编辑
字节跳动_QA
Edgestr:这种的写代码最狠了
点赞 评论 收藏
分享
02-02 19:07
已编辑
河南师范大学 算法工程师
用微笑面对困难:一般5年老工都不太敢写“精通python”至少要达到noi的级别 兄弟你要是真精通大厂秒进的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务