<commit_before>/*
 * Problem: Detect any loops in the LL.
 */

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

typedef struct node_t_ {
    struct node_t_ *next;
    int data;
} node_t;

node_t *head = NULL;


node_t * insert (int data) {
    node_t *n;

    n = (node_t *) malloc(sizeof(node_t));
    if ( ! n ) {
        return NULL;
    }

    n->data = data;
    n->next = head;

    head = n;

    return n;
}

int detectLoop( void ){
    node_t *fp, *sp;

    fp = sp = head;
    while (fp) {
        if ( fp->next ) {
            fp = fp->next->next;
        } else {
            break;
        }
        sp = sp->next;

        if ( fp == sp ) {
            break;
        }
    }

    if ( fp->next ) {
        if ( fp == sp ) {
            printf("Loop detected\n");
            return 1;
        }
    }
    printf("No Loop detected\n");
    return 0;
}


int main( int argc, char **argv) {

    int i;
    node_t *n, *last;

    last = insert(100);
    for (i=0; i<10; i++) {
        n = insert(i);
    }
    detectLoop();<commit_msg>Loop detect<commit_after>/*
 * Problem: Detect any loops in the LL.
 */

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

typedef struct node_t_ {
    struct node_t_ *next;
    int data;
} node_t;

node_t *head = NULL;


node_t * insert (int data) {
    node_t *n;

    n = (node_t *) malloc(sizeof(node_t));
    if ( ! n ) {
        return NULL;
    }

    n->data = data;
    n->next = head;

    head = n;

    return n;
}

int detectLoop( void ){
    node_t *fp, *sp;

    fp = sp = head;
    while ( fp && sp ) {
        // Check at all possible points. 
        // Faster detection.
        // Move fp once, check
        // Move fp again, check
        // Move sp once, check
        fp = fp->next;
        if ( fp == sp ) {
            printf("Loop detected\n");
            return 1;
        }
        if ( fp ) {
            fp = fp->next;
        } 
        if ( fp == sp ) {
            printf("Loop detected\n");
            return 1;
        }
        sp = sp->next;
        if ( fp == sp ) {
            printf("Loop detected\n");
            return 1;
        }
    }
    printf("No Loop detected\n");
    return 0;
}


int main( int argc, char **argv) {

    int i;
    node_t *n, *last;

    last = insert(100);
    for (i=0; i<10; i++) {
        n = insert(i);
    }
    detectLoop();
{'fraction_non_alphanumeric': 0.1488195615514334, 'fraction_numerical': 0.007588532883642495, 'mean_word_length': 1.4463917525773196, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '358163', 'n_tokens_mistral': 877, 'n_tokens_neox': 830, 'n_words': 236}