The problem. Given head, determine whether the linked list has a cycle: some node whose next arrow points back to an earlier node, so walking the list would go round forever. Return true if there is a cycle, otherwise false.
Input: head = [3, 2, 0, -4], tail connects to index 1
Output: true
Input: head = [1], no cycle
Output: falseYou can't just walk until null — with a cycle, null never comes. You need a way to notice that you've been somewhere before.
Free account
Sign up to read the rest of this lesson: 5 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come