141. Linked List Cycle#2(Done)

来源:互联网 发布:最优化理论 编辑:程序博客网 时间:2024/06/14 06:03

Solution

/** * 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 || head.next == null) {            return false;        }        ListNode slow = head;        ListNode fast = head.next;        while (fast.next != null && fast.next.next != null) {            if (slow == fast) {                return true;            }            slow = slow.next;            fast = fast.next.next;        }        return false;    }}

Problem#1
* 最好用不可变对象当做HashMap的Key
* 跑圈方法,写总结

0 0
原创粉丝点击