Loop List

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.
时间: 2024-08-11 17:20:41

Loop List的相关文章

mysql while,loop,repeat循环,符合条件跳出循环

1.while循环 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test_while`$$ CREATE PROCEDURE `sp_test_while`( IN p_number INT, #要循环的次数 IN p_startid INT #循环的其实值 ) BEGIN DECLARE v_val INT DEFAULT 0; SET v_val=p_startid; outer_label: BEGIN #设置一个标记 WHILE v_val<=p_

hdu 5348 MZL&amp;#39;s endless loop

给一个无向图(事实上是有向的.可是没有指定边的方向),你须要指定边的方向,使得每一个点入度和出度相差不超过1. 事实上就是找很多条路径.合起来能走完这个图..先统计各个顶点的度.度为奇数必是起点或终点,否则是中间点或者同为起点和终点. 邻接表建图(建双向),先从每一个奇数度顶点出发找路径,再从偶数度顶点出发找路径.经过的边要删去不然超时. #include <iostream> #include <cstring> #include <cstdio> #include

王爽《汇编语言》第三版 第五章 [BX]和loop指令

5.1 [bx] mov ax,[bx]功能:bx 中存放的数据作为一个偏移地址EA ,段地址SA 默认在ds 中,将SA:EA处的数据送入ax中. 5.2 Loop指令 指令的格式是:loop 标号,CPU 执行loop指令的时候,要进行两步操作: 1.(cx)=(cx)-1: 2.判断cx中的值,不为零则转至标号处执行程序,如果为零则向下执行. 通常我们用loop指令来实现循环功能,cx 中存放循环次数. 5.3 在Debug中跟踪用loop指令实现的循环程序 5.4 Debug和汇编编译器

archlinux 加载loop模块,且设定loop设备个数

如果loop模块没有编译进内核就要先加载loop模块 modprobe loop 然后更改/etc/modprobe.d/modprobe.conf(有些文章写是在/etc/modprobe.conf,但是我试验是在此目录下) options loop max_loop=64 之后就可以重启系统,然后执行 modprobe loop 我们就可以看到loop设备增加为64个了.

/dev目录下的loop和ram

我们使用top命令会发现关于磁盘的内容中,可以看到loop和ram伪设备文件,但是它们是做什么的呢? 首先我们要先说一下在Linux的中I/O设备分两类,也就是块设备和字符设备.它们最简单的区分就是对数据的读取顺序: 字符设备:连续的数据流,程序要顺序读取,不能随机跳跃式读取,比如键盘输入的东西 块设备:可以随机访问数据,比如硬盘.光盘.U盘等,通常以块为单位的进行存取,块这个概念是操作系统层面的,落实到硬盘其实硬盘存取数据的最小单位是扇区(512字节).块通常是由1个或多个扇区组成. 上述两种

How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for loop?

Scala List/sequence FAQ: How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for loop? There are a number of ways to iterate over a Scala List using theforeach method (which is available to Scala sequences li

禁用nested loop join里的spool

禁用nested loop join里的spool 转载自: https://blogs.msdn.microsoft.com/psssql/2015/12/15/spool-operator-and-trace-flag-8690/ https://answers.sqlperformance.com/questions/698/lazy-spool.html 在nested loop join里常常会看到一个spool操作符 这个spool 又名'performance spool',目的是

Oracle PLSQL Demo - 05.WHILE循环[WHILE LOOP]

declare v_sal number := 6000; begin while (v_sal < 8000) loop v_sal := v_sal + 1; dbms_output.put_line(v_sal); end loop; end;

Oracle PLSQL Demo - 04.数字FOR LOOP循环[NUMBERABLE (FOR) LOOP]

declare v_display varchar2(10); begin for i in 1 .. 100 loop for j in reverse 1 .. 10 loop dbms_output.put_line(i || ' - ' || j); end loop; end loop; end;

Oracle PLSQL Demo - 07.LOOP循环,以EXIT WHEN退出[EXIT in LOOP]

declare v_sal number := 6000; begin loop v_sal := v_sal + 1; dbms_output.put_line(v_sal); exit when v_sal = 8000; end loop; end;