0
0
CHow-ToBeginner · 3 min read

How to Detect Cycle in Linked List in C: Simple Method

To detect a cycle in a linked list in C, use Floyd’s Cycle Detection algorithm with two pointers: slow moves one step and fast moves two steps. If they meet, a cycle exists; if fast reaches the end, no cycle is present.
📐

Syntax

Use two pointers to traverse the linked list:

  • slow: moves one node at a time
  • fast: moves two nodes at a time

If fast or fast->next becomes NULL, the list has no cycle. If slow equals fast, a cycle is detected.

c
struct Node {
    int data;
    struct Node* next;
};

int hasCycle(struct Node* head) {
    struct Node *slow = head, *fast = head;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            return 1; // cycle found
        }
    }
    return 0; // no cycle
}
💻

Example

This example creates a linked list with a cycle and uses the detection function to check it.

c
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int hasCycle(struct Node* head) {
    struct Node *slow = head, *fast = head;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            return 1; // cycle found
        }
    }
    return 0; // no cycle
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));
    struct Node* third = malloc(sizeof(struct Node));

    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = second; // cycle here

    if (hasCycle(head)) {
        printf("Cycle detected in the linked list.\n");
    } else {
        printf("No cycle in the linked list.\n");
    }

    // Freeing nodes is skipped due to cycle
    return 0;
}
Output
Cycle detected in the linked list.
⚠️

Common Pitfalls

Common mistakes include:

  • Not checking if fast or fast->next is NULL before moving pointers, causing crashes.
  • Using only one pointer, which cannot detect cycles reliably.
  • Forgetting to handle empty lists (head == NULL).

Always ensure safe pointer moves and proper loop conditions.

c
/* Wrong approach: moving fast pointer without NULL checks */
int wrongHasCycle(struct Node* head) {
    struct Node *slow = head, *fast = head;
    while (fast != NULL && fast->next != NULL) { // fixed unsafe condition
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            return 1;
        }
    }
    return 0;
}

/* Correct approach includes NULL checks */
int correctHasCycle(struct Node* head) {
    struct Node *slow = head, *fast = head;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            return 1;
        }
    }
    return 0;
}
📊

Quick Reference

  • Use two pointers: slow (1 step), fast (2 steps).
  • Check fast != NULL && fast->next != NULL before moving pointers.
  • If slow == fast, cycle exists.
  • If fast reaches NULL, no cycle.

Key Takeaways

Use Floyd’s Cycle Detection with two pointers moving at different speeds to detect cycles.
Always check pointers for NULL before advancing to avoid crashes.
If the two pointers meet, a cycle exists; if the fast pointer reaches the end, no cycle is present.
Handle empty lists by checking if the head is NULL before processing.
Avoid using only one pointer or unsafe pointer moves to prevent errors.