leedcode做题总结,题目Linked List Cycle I/II 13/10/28-30
来源:互联网 发布:广州云计算培训 编辑:程序博客网 时间:2024/05/16 23:44
这两道题很简单,思路都是两个节点从head开始,一个一次走一步,另一个走两步,如果遇到了就说明有循环。
第二题是要寻找循环的开始,由于在一个环中,如果两个节点一个一次1步,另一个一次2步,则他们永远在环的某个固定节点相遇。所以我们把换前面的链扳弯贴合在环上,则链表头的位置即为环中两点相遇的地方。所以此点到环开始的地方和链表头到环开始的地方的距离是相同的,所以先找到相遇点,再将其中一个移到链表头,同时以1次1步前进,则会在环的开端相遇。
public ListNode detectCycle(ListNode head) { if (head==null)return null; ListNode m = head; ListNode n = head; while (true){ if (m.next==null) return null; if (n.next==null) return null; if (n.next.next==null) return null; m=m.next; n=n.next.next; if(m.equals(n))break; } m=head; while (!m.equals(n)){ m=m.next; n=n.next; } return m; }
0 0
- leedcode做题总结,题目Linked List Cycle I/II 13/10/28-30
- LeedCode:Linked List Cycle II
- leedcode做题总结,题目Pascal's Triangle I/II-------2012/10/28
- leedcode做题总结,题目Flatten Binary Tree to Linked List 2012/10/14
- [LeedCode OJ]#142 Linked List Cycle II
- Linked List Cycle I & II
- Linked List Cycle I&&II
- Linked List Cycle I / II
- Linked List Cycle I II
- Linked List Cycle I&II
- leedcode做题总结,题目Reorder List 13/11/12
- leedcode做题总结, 题目Find Minimum in Rotated Sorted Array I/II
- leedcode做题总结,题目Populating Next Right Pointers in Each NodeI/II---------- 2012/10/28
- leedcode做题总结,题目Path SumI/II-------- 2012/10/13-14
- leetcode题目:Linked List Cycle和Linked List Cycle II
- [LeedCode]Find the Duplicate Number/Linked List Cycle II
- JAVA-Linked List Cycle I&&Linked List Cycle II
- Linked List Cycle I,II, Partition List
- linux环境变量
- Netstat命令详解(windows下)
- POJ 3579 Median 查找中间值 二分
- linux下创建文件过多,导致inode用光
- 自定义 UITabbarController
- leedcode做题总结,题目Linked List Cycle I/II 13/10/28-30
- windows net 命令详解
- Codeforces 445A DZY Loves Chessboard(水题)
- 数据库的最简单实现
- C++类可以不显示声明构造函数,在创建类对象时编译器自动调用默认的构造函数
- POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】
- Javascript的预编译和执行顺序
- C#用openfiledialog文件和savefileDialog打开和保存文件
- openfire 安装部署