Linked List Cycle Problems

来源:互联网 发布:java程序员求职经验 编辑:程序博客网 时间:2024/06/06 07:50

The Linked List Cycle problem is:

Given a linked list, determine if it has a cycle in it. Can you solve it without using extra space?

A follow up problem is:

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Can you solve it without using extra space?

The first problem can be solved by the Floyd’s Cycle-Finding Algorithm:

Traverse linked list using two pointers: move one pointer by one and other pointer by two. If these pointers meet at some node then there is a loop. If pointers do not meet then linked list doesn’t have loop.

As for the second problem, it is also based on the Floyd’s Cycle-Finding Algorithm. However, after detecting loop, we need to find the starting point of the cycle. The most convinient way is to use two pointers, one points to the head node, and the other points to the node that pointers meet when detecting loop. Then move both the two pointers by one respectively in each iteration, until they meet. The node they meet is the starting point of the cycle.

- See more at:http://bo-yang.github.io/2014/07/13/linked-list-cycle/#sthash.I1lYsJyS.dpuf

http://bo-yang.github.io/2014/07/13/linked-list-cycle

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 合同中付款方式错怎么办? 优步付款方式无效怎么办 工程付款方式变更没有合同怎么办 银行账号被锁了怎么办? 街电押金退不了怎么办 佣金宝账号忘了怎么办 如果汇款汇错了怎么办 手机汇款汇错了怎么办 汇款时少了数字怎么办 打过流脑后发烧怎么办 甲醛公司除完后怎么办 发票系统导出的xml 怎么办 新买的书包味道太大怎么办 alt+a截图热键冲突怎么办 白背心领发黄了怎么办 房屋装修后出现质量问题怎么办 华为p9手机音量小怎么办 华为畅享8玩游戏卡怎么办 华为畅享7玩游戏卡怎么办 华为p9升级以后屏幕失灵怎么办 荣耀9青春版玩游戏卡怎么办 华为p9屏幕不亮了怎么办 华为p9入水黑屏怎么办 农信密码忘记了怎么办 小米4g信号差怎么办 手机的调频调制器坏了怎么办 调制解调器的灯一直闪怎么办 691宽带用户名和密码无效怎么办 房间里有狐臭味怎么办 吃冰冻水果胃特别痛怎么办 小米5s屏幕闪烁怎么办 系统重装重启了怎么办 华为p10开不了机怎么办 眼镜腿夹的头疼怎么办 摩托车车头锁坏了怎么办 夏天骑摩托车戴头盔热怎么办 头盔玻璃罩边固定老是掉怎么办 电动车不戴头盔被扣车怎么办 郴州骑电动车没戴头盔怎么办 配置数据源时发现两个版本怎么办 微信占用内存3g怎么办