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

判断链表中是否有环

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

import java.util.*;
/**
 * 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;
        }
        if(head.next == null){
            return false;
        }
        List<ListNode> arr = new ArrayList<>();
        int t = 0;
        while(head!=null){
            arr.add(head);
            head = head.next;
            if(find(arr,head))
            {
                t = 1;
                break;
            }
        }
        if(t == 1)
            return true;
        else
            return false;
    }
    public boolean find(List<ListNode> arr,ListNode target){
        
            if(arr.contains(target)){
                return true;
            }else{
                return false;
            }
        }
}

全部评论

相关推荐

2025-12-18 18:23
深圳大学 前端工程师
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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