判断链表中是否有环
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;
}
}
全部评论
相关推荐
点赞 评论 收藏
分享
很奥的前端仔:如果你接了offer 临时又说不去 hr确实要多做一些工作。 当然如果是接offer之前当我没说
点赞 评论 收藏
分享
曼迪匹艾公司福利 131人发布