Skip to content

Commit 1776fc6

Browse files
committed
修复只有两个节点的链表
1 parent f4ba1de commit 1776fc6

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/main/java/com/crossoverjie/algorithm/LinkLoop.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public boolean isLoop(Node node){
3737
return true ;
3838
}
3939

40+
if (fast.next == null){
41+
return false ;
42+
}
4043
//slow走慢点 fast走快点
4144
slow = slow.next ;
4245
fast = fast.next.next ;

src/test/java/com/crossoverjie/algorithm/LinkLoopTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public void isLoop2() throws Exception {
4242
Assert.assertEquals(loop,true);
4343
}
4444

45+
/**
46+
* 无环
47+
* @throws Exception
48+
*/
49+
@Test
50+
public void isLoop3() throws Exception {
51+
LinkLoop.Node node2 = new LinkLoop.Node("2") ;
52+
LinkLoop.Node node1 = new LinkLoop.Node("1") ;
53+
54+
node1.next = node2 ;
55+
56+
57+
LinkLoop linkLoop = new LinkLoop() ;
58+
boolean loop = linkLoop.isLoop(node1);
59+
Assert.assertEquals(loop,false);
60+
}
61+
4562
}

0 commit comments

Comments
 (0)