102

来源:互联网 发布:国密sm2算法程序 编辑:程序博客网 时间:2024/04/29 16:29

5.26

好久没写代码了。今天就瞎写几道简单的吧。链表还是比较容易的呢。

/** * 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        HashMap<ListNode,Integer> map = new HashMap<ListNode,Integer>();        while(head != null){            if(!map.containsKey(head)){                map.put(head,1);            }            else{                return true;            }            head = head.next;        }        return false;    }}