Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Linked List Cycle II problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Linked List Cycle II problem solution, we have given a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if some node in the list can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that the tail’s next pointer is connected to. Note that pos is not passed as a parameter. Notice that you should not modify the linked list.

Leetcode Linked List Cycle II problem solution

Leetcode Linked List Cycle II problem solution in Python.

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        dict1 = dict()
        current = head
        while(current):
            if(current in dict1):
                return True
            dict1[current] = None
            current = current.next
        return False

Linked List Cycle II problem solution in Java.

public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        do {
            if (fast == null || fast.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        } while (fast != slow);
        
        slow = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

Problem solution in C++.

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *resultNode;
        ListNode *slow = head;
        ListNode *fast = head;
        
        if(head == nullptr){
            resultNode = nullptr;
        }
        
        while(fast !=nullptr && fast->next !=nullptr && slow !=nullptr){
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast){
                slow = head;
                while(slow != fast){
                    slow = slow->next;
                    fast = fast->next;
                }
                resultNode = slow;
                return resultNode;   
            }
        }
        
        resultNode = nullptr;
        return resultNode;
    }
};

Problem solution in C.

struct ListNode *detectCycle(struct ListNode *head) {

if (head == NULL || head->next == NULL) {
    return NULL;
}

struct ListNode* slow = head;
struct ListNode* fast = head;

while (fast != NULL && fast->next != NULL) {
    
    slow = slow->next;
    fast = fast->next->next;
    
    if (slow == fast) {
        break;
    } 
}

if (fast == NULL || fast->next == NULL) { 
    return NULL;
}

struct ListNode* p1 = head;
struct ListNode* p2 = slow;

while (p1 != p2) {
    p1 = p1->next;
    p2 = p2->next;
}

return p1;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes