Linked List Cycle

来源:互联网 发布:棋牌游戏透视软件 编辑:程序博客网 时间:2024/05/16 17: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 sn = head, fn = head.next;        while (fn != null && fn.next != null) {            if (fn == sn) {                return true;            }            sn = sn.next;            fn = fn.next.next;        }        return false;    }}

0 0
原创粉丝点击