answersLogoWhite

0


Best Answer

A possibility is tagging. Example:

typedef struct ListElem {

struct ListElem *next;

int tag;

int value;

} ListElem;

int Check (ListElem *l)

{

static int tagn= 0;

++tagn;

for (; l; l= l->next) {

if (l->tag==tagn) return -1; /* loop */

l->tag= tagn;

}

return 0;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you detect a loop in a linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

W difference between a linear linked list and a circular linked list?

I would say that there is no such thing as a circular queue. The point of a circular data structure is to allow the end to loop around to the beginning. Since you can only remove items from the beginning of a queue or add them to the front, having these two items linked has no purpose nor benefit.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


What is the difference between linked list and Ordinary list?

A list is an abstract data structure, usually defined as an ordered collection of data. A linked list refers to a specific implementation of a list in which each element in the list is connected (linked) to the next element.


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


Difference between circular and single linklist?

A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.

Related questions

How do you detect the linked list is full?

A linked list cannot be 'full'. You might run out of memory, though.


How can you find out if a linked list is circular?

Pseudocode for detecting loops in a linked list: // keep track of which linked list nodes we've visited set nodesVisited // our list linkedlist list // current working node node current = list.root while current.next is not null if nodesVisited contains current.next we found a loop! else nodesVisited.add( current ) current = current.next // if we get here without finding a loop, then there are no loops Complete Working code And illustrative pictures can be found here about linked lists http://www.programmerinterview.com/index.php/data-structures/how-to-find-if-a-linked-list-is-circular-or-has-a-cycle-or-it-ends/


W difference between a linear linked list and a circular linked list?

I would say that there is no such thing as a circular queue. The point of a circular data structure is to allow the end to loop around to the beginning. Since you can only remove items from the beginning of a queue or add them to the front, having these two items linked has no purpose nor benefit.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


What is the difference between doubly linked list and circular linked list?

A doubly linked list allows traversal in both directions (forward and backward) by having each node point to both its next and previous nodes. A circular linked list is a type of linked list where the last node points back to the first node, forming a circular structure. This allows continuous traversal through the elements without a definitive end.


Which is the easy insertion operator single linked-list or double-linked list?

It is easier to insert into a singly linked list.


What is the difference between linked list and Ordinary list?

A list is an abstract data structure, usually defined as an ordered collection of data. A linked list refers to a specific implementation of a list in which each element in the list is connected (linked) to the next element.


What is non-integer linked list?

Linked list of strings, for example.


How you can delete the node with smallest data from a linked list?

I'm currently doing the same homework problem in c++, my best guess right now is to use some function such as max element size or sizeof(object) and then if and then statements or a for loop to go through the list to delete the smallest linked node.


what are the various operations in linked list which can be performed on linked list?

Some common operations that can be performed on a linked list include inserting a node, deleting a node, searching for a specific node, traversing the list, and updating a node's value. Other operations may include reversing the list, merging two lists, sorting the list, and finding the length of the list.


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


Difference between circular and single linklist?

A regular linked list will have a pointer to the start of the list, with each node pointing to the next node, and finally the last node points to NULL. In a circular linked-link, the last node will point to the first node, making the list circular. This requires extra checks to ensure that you don't end up going into an infinite loop while traversing the list.