All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0141_linked_list_cycle.solution.go Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package s0141_linked_list_cycle

// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
// #2024_03_20_Time_7_ms_(69.12%)_Space_4.6_MB_(82.42%)

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func hasCycle(head *ListNode) bool {
	slow, fast := head, head
	for fast != nil && fast.Next != nil {
		if fast.Next == slow {
			return true
		}
		fast = fast.Next.Next
		slow = slow.Next
	}
	return false
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy