Created
December 15, 2022 23:34
-
-
Save abdulrahmanAlotaibi/d23658de46186a2370e6f69edf4cbff8 to your computer and use it in GitHub Desktop.
Linkedlists : Linked List Cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* type ListNode struct { | |
* Val int | |
* Next *ListNode | |
* } | |
*/ | |
func hasCycle(head *ListNode) bool { | |
dummy := head | |
arr := []*ListNode{} | |
for dummy != nil { | |
for _, n := range arr { | |
if dummy.Next == n { | |
return true | |
} | |
} | |
arr = append(arr, dummy) | |
dummy = dummy.Next | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment