判断链表中是否有环
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode head1=head;
ListNode head2=head;
if(head==null){
return false;
}
while(head1!=null&&head1.next!=null){
head2=head2.next;
head1=head1.next.next;
if(head1==head2)
return true;
}
return false;
}
}
public boolean hasCycle(ListNode head) {
ListNode head1=head;
ListNode head2=head;
if(head==null){
return false;
}
while(head1!=null&&head1.next!=null){
head2=head2.next;
head1=head1.next.next;
if(head1==head2)
return true;
}
return false;
}
}
全部评论
相关推荐
09-18 20:41
门头沟学院 Java 点赞 评论 收藏
分享
查看27道真题和解析