博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Loop List
阅读量:5142 次
发布时间:2019-06-13

本文共 855 字,大约阅读时间需要 2 分钟。

Loop List is very common in interview. This article we give a more strict short statement about its solving.

 

One method to check if there's a loop in a list is to use two speed pointers. But most of articles did not give a proof.

 

Assume we have a list:

ListNode* head;

Then, we set two pointers start from the head, and one pointer added by 1 step, the other added by 2 steps:

ListNode* p1 = head, *p2 = head;...p1 = (p1 -> next) -> next;p2 = p2 -> next;

Now, we need to proof that, if there's a loop in the list, p1 will meet p2.

  1. p1 obviously will exceed p2.
  2. As p1 can only added two steps every time, there're only two relative positions between p1 and p2.
    • p1 just before p2. Then as p1 updates first, p2 updates second, they'll meet.
    • There's a node between p1 and p2. As p1 updates first, they'll meet.

 

转载于:https://www.cnblogs.com/kid551/p/4114546.html

你可能感兴趣的文章
+new Date()的用法
查看>>
Git 使用
查看>>
JavaScript 语句 while
查看>>
Function eregi() is deprecated (解决方法)
查看>>
win7 iis7 HTTP 错误 401.3 - Unauthorized
查看>>
Oracle注意事项
查看>>
容器(docker)内运行Nginx
查看>>
WinCE应用程序开发---打开或另存为对话框
查看>>
央视影音 for Mac 1.2.1 中文版 – CCTV和地方卫视直播软件
查看>>
谈谈市面上无线路由器的性能和芯片
查看>>
PHP 开发工具【2】
查看>>
『数据仓库』学习记录(1)
查看>>
CI Weekly #15 | 据说新版 flow.ci Dashboard 界面很酷
查看>>
短信编码总结
查看>>
了解HTML和Css样式
查看>>
关于settimer的一些新认识
查看>>
[转]ExtJs4 笔记(13) Ext.menu.Menu 菜单、Ext.draw.Component 绘图、Ext.resizer.Resizer 大小变更...
查看>>
1-5-06:奥运奖牌计数
查看>>
Windows下Python连接sqlite3数据库
查看>>
Javascript 类与静态类的实现(续)
查看>>