leetcode之Linked List Cycle

来源:互联网 发布:什么叫java参数传递 编辑:程序博客网 时间:2024/05/02 02:22
/** * 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) {        ListNode first=head;        ListNode second=head;        if(head == null || head.next == null){            return false;        }        first=first.next;        second=second.next.next;        while(first!=second && first!=null && second!=null){            first=first.next;            if(second.next == null){                return false;            }              second=second.next.next;        }        if(first == null || second == null){            return false;        }        return true;    }

0 0
原创粉丝点击