Linked List Cycle

来源:互联网 发布:c语言windows窗口程序 编辑:程序博客网 时间:2024/06/15 20:05

基础题,关注其拓展,

一个不错的参考点击打开链接

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



0 0
原创粉丝点击