{"id":22513715,"url":"https://github.com/emahtab/linked-list-cycle","last_synced_at":"2026-02-04T01:09:25.813Z","repository":{"id":79525609,"uuid":"239339319","full_name":"eMahtab/linked-list-cycle","owner":"eMahtab","description":"Linked List Cycle","archived":false,"fork":false,"pushed_at":"2021-08-15T14:07:55.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T13:53:21.110Z","etag":null,"topics":["detect-cycle","hashset","leetcode","linked-list","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-09T16:53:55.000Z","updated_at":"2021-08-15T14:07:58.000Z","dependencies_parsed_at":"2023-05-10T17:16:10.000Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/linked-list-cycle","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/linked-list-cycle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flinked-list-cycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flinked-list-cycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flinked-list-cycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flinked-list-cycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/linked-list-cycle/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flinked-list-cycle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29063437,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T00:26:14.114Z","status":"ssl_error","status_checked_at":"2026-02-04T00:23:06.435Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["detect-cycle","hashset","leetcode","linked-list","problem-solving"],"created_at":"2024-12-07T03:14:09.070Z","updated_at":"2026-02-04T01:09:25.776Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Linked List Cycle\n## https://leetcode.com/problems/linked-list-cycle\n\nGiven a linked list, determine if it has a cycle in it.\n\nTo represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.\n\n```\nExample 1:\n\nInput: head = [3,2,0,-4], pos = 1\nOutput: true\nExplanation: There is a cycle in the linked list, where tail connects to the second node.\n\n\nExample 2:\n\nInput: head = [1,2], pos = 0\nOutput: true\nExplanation: There is a cycle in the linked list, where tail connects to the first node.\n\n\nExample 3:\n\nInput: head = [1], pos = -1\nOutput: false\nExplanation: There is no cycle in the linked list.\n```\n\n**Follow up:**\nCan you solve it using O(1) (i.e. constant) memory?\n\n## Implementation 1 : Time : O(n), Space : O(n)\n```java\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode(int x) {\n *         val = x;\n *         next = null;\n *     }\n * }\n */\npublic class Solution {\n    public boolean hasCycle(ListNode head) {\n        if(head == null)\n            return false;\n        Set\u003cListNode\u003e set = new HashSet\u003c\u003e();\n        ListNode current = head;\n        while(current != null) {\n            if(set.contains(current))\n                return true;\n            set.add(current);\n            current = current.next;\n        }\n        return false;\n    }\n}\n```\n\n\n\n## Implementation 2: (Fast \u0026 Slow will eventually meet :handshake: )\n\n```java\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode(int x) {\n *         val = x;\n *         next = null;\n *     }\n * }\n */\npublic class Solution {\n    public boolean hasCycle(ListNode head) {\n        // If there is no node or just one node in the linked list\n        if (head == null || head.next == null) {\n            return false;\n        }\n        ListNode slow = head;\n        ListNode fast = head.next;\n        while (slow != fast) {\n            if (fast == null || fast.next == null) {\n                return false;\n            }\n            slow = slow.next;\n            fast = fast.next.next;\n        }\n        return true;\n    }\n}\n\n```\n## Implementation 2: Just little different (Fast \u0026 Slow will eventually meet :handshake: )\n\n```java\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode(int x) {\n *         val = x;\n *         next = null;\n *     }\n * }\n */\npublic class Solution {\n    public boolean hasCycle(ListNode head) {\n        if(head == null)\n            return false;\n        \n        ListNode slow = head;\n        ListNode fast = head;\n        \n        while(fast != null \u0026\u0026 fast.next != null) {\n            slow = slow.next;\n            fast = fast.next.next;\n            if(slow == fast)\n                return true;\n            \n        }\n        return false;\n    }\n}\n```\n\n# References :\n1. https://leetcode.com/articles/linked-list-cycle\n2. https://www.youtube.com/watch?v=6OrZ4wAy4uE\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flinked-list-cycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Flinked-list-cycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flinked-list-cycle/lists"}