# Detecting a loop in the linklist?

388 views

Algorithm:
1. Traverse linked list using two pointers called slow pointer and fast pointer.
2. Move slow pointer by one in each iteration and fast pointer by two.
3. If these pointers meet then there is a loop. If pointers do not meet then linked list doesn’t have loop.

Sample Code

``````struct node
{
int data;
struct node *next;
};

int length= 0;

int list_size()
{
printf("\n No. of elements in List : %d", length);
return length;
}

struct node * create_node(int data)
{
struct node *new;

new = (struct node*)malloc(sizeof(struct node));
new->next = null;
new->data = data;

length++;

return new;
}

{
struct node *new;
new = create_node(data);

{
}
else
{
}
}

void display_list()
{

{
printf("List is empty");
return;
}

{
}
}

{

while(slow_pointer && fast_pointer && fast_pointer->next )
{
slow_pointer = slow_pointer->next;
fast_pointer = fast_pointer->next->next;

if (slow_pointer == fast_pointer)
{
printf("Found Loop");
return 1;
}
}
return 0;
}
``````
posted Sep 8, 2014

## Related Articles

This program only first n elements of a linked list as compare to whole reverse as discussed in the http://tech.queryhome.com/57009/reversing-a-linklist-in-c . In this program we are manipulating the pointers only in place of data swap.

Sample Code

``````struct node
{
int data;
struct node *next;
};

int length= 0;

int list_size()
{
printf("\n No. of elements in the list: %d", length);
return length;
}

struct node * create_node(int data)
{
struct node *new;

new = (struct node*)malloc(sizeof(struct node));
new->next = null;
new->data = data;

length++;

return new;
}

{
struct node *new;
new = create_node(data);

{
}
else
{
}
}

void display_list()
{

{
printf("List is empty");
return;
}

{
}
}

void reverse(struct node **head, int n)
{
struct node *p, *q, *r;

p = q = r = *head;

if (n > 0) // If N is zero then no need to reverse
{
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;

while (n > 0 && p != NULL) // Run a loop for n times only
{
r = q;
q = p;
p = p->next;
q->next = r;
n--;
}
}
}
``````

W linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a link to the next node in the sequence.

Reversing a link list can be done in two ways one by swapping the data values and another is by manipulating only the pointers, in this article I am discussing only the pointer manipulation mechanism in recursive and non-recursive ways.

Sample Code

``````struct node
{
int data;
struct node *next;
};

int length= 0;

int list_size()
{
printf("\n No. of elements in the list: %d", length);
return length;
}

struct node * create_node(int data)
{
struct node *new;

new = (struct node*)malloc(sizeof(struct node));
new->next = null;
new->data = data;

length++;

return new;
}

{
struct node *new;
new = create_node(data);

{
}
else
{
}
}

void display_list()
{

{
printf("List is empty");
return;
}

{
}
}

void reverse_list()
{
struct node *p, *q, *r;

p = q = r = head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;

while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
}

{