Skip to content

Commit 88a189c

Browse files
committed
Update: Linked List Cycle
1 parent b4b5301 commit 88a189c

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

problems/linked-list-cycle/linked_list_cycle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import . "github.com/openset/leetcode/internal/kit"
1010
* }
1111
*/
1212
func hasCycle(head *ListNode) bool {
13-
for p1, p2 := head, head; p1 != nil; p1 = p1.Next {
14-
if p2 != nil && p2.Next != nil {
13+
for p1, p2 := head, head; p1 != nil && p2 != nil; p1 = p1.Next {
14+
if p2.Next != nil {
1515
if p2 = p2.Next.Next; p1 == p2 {
1616
return true
1717
}

problems/linked-list-cycle/linked_list_cycle_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func TestHasCycle(t *testing.T) {
2424
pos: 0,
2525
expected: true,
2626
},
27+
{
28+
input: []int{1, 2, 3, 4, 5},
29+
pos: -1,
30+
expected: false,
31+
},
2732
{
2833
input: []int{1},
2934
pos: -1,

0 commit comments

Comments
 (0)