141. Linked List Cycle
来源:互联网 发布:手机指南针软件 编辑:程序博客网 时间:2024/05/16 18:57
判断是否为循环链表。使用快慢指针,p1=p1.next , p2=p2.next.next 如果为循环链表,p2会追上p1.
Given a linked list, determine if it has a cycle in it.
public class Solution {
public boolean hasCycle(ListNode head) {if(head==null)
return false;
ListNode p1=head;
if(head.next==null)
return false;
ListNode p2=head;
p1=p1.next;
p2=p2.next.next;
while(p1!=null&&p2!=null){
p1=p1.next;
if(p2.next==null)
return false;
else
p2=p2.next.next;
if(p1.equals(p2))
return true;
}
return false;
}
}
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 【git】github基础
- Swift3.0 访问通讯录
- iOS10:CallKit的简单应用
- SRS产品规划
- poj3481 Double Queue(set模拟or splay)
- 141. Linked List Cycle
- C#复制数组的两种方式,以及效率比较
- Undefined symbols for architecture i386: "_OBJC_CLASS_$_xxxxx", referenced from:
- 雾化
- Effective C++阅读笔记(二):设计与声明
- JAVA学习之创建一个小的命令行程序以及创建一个小的有对话界面的程序
- tableview 编辑状态下,UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert 多选的时候,替换圆圈图片
- 欢迎使用CSDN-markdown编辑器
- nyoj 还是打印菱形