Java实现-带环链表

来源:互联网 发布:meanshift 聚类算法 编辑:程序博客网 时间:2024/06/05 03:38


/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {    /**     * @param head: The first node of linked list.     * @return: True if it has a cycle, or false     */    public boolean hasCycle(ListNode head) {          // write your code here        if(head==null||head.next==null){return false;}ListNode first=head;ListNode second=head;while(second.next!=null&&second.next.next!=null){if(first.next==second.next.next){return true;}else{first=first.next;second=second.next.next;}}return false;    }}


原创粉丝点击