Loop in a linked list - C

This is a discussion on Loop in a linked list - C ; How to find whether there is a loop in a linked list. suppose I have a linked list in the shape of numeral 9(next pointer of last node points to somewhere in the middle of list instead of pointing to ...

+ Reply to Thread
Results 1 to 3 of 3

Loop in a linked list

  1. Default Loop in a linked list

    How to find whether there is a loop in a linked list. suppose I have a
    linked list in the shape of numeral 9(next pointer of last node points to
    somewhere in the middle of list instead of pointing to NULL)



  2. Default Re: Loop in a linked list

    In article <fdfddh$obh$1@news4.fe.internet.bosch.com>,
    Rohit kumar Chandel <rohitkumar.chandel@in.bosch.com> wrote:
    >How to find whether there is a loop in a linked list. suppose I have a
    >linked list in the shape of numeral 9(next pointer of last node points to
    >somewhere in the middle of list instead of pointing to NULL)


    A traditional technique is to have two pointers go along the list, one
    twice as fast as the other. If there's a loop, they will eventually
    be equal. If there isn't, the fast one will run off the end.

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

  3. Default Re: Loop in a linked list

    In article <fdfddh$obh$1@news4.fe.internet.bosch.com>,
    Rohit kumar Chandel <rohitkumar.chandel@in.bosch.com> wrote:
    >How to find whether there is a loop in a linked list. suppose I have a
    >linked list in the shape of numeral 9(next pointer of last node points to
    >somewhere in the middle of list instead of pointing to NULL)


    Have a 'mark' field in each node. Start at the first member of
    the list and set the mark field, and then traverse to the children.
    If you encounter a node which already has the 'mark' field set,
    then you have a loop in the list.
    How to clear all the marks is left as an exercise for the reader ;-)

    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers

+ Reply to Thread

Similar Threads

  1. Building a linked list
    By Application Development in forum Java
    Replies: 20
    Last Post: 09-22-2007, 09:15 AM
  2. linked list...
    By Application Development in forum c++
    Replies: 11
    Last Post: 06-22-2007, 04:29 AM
  3. Linked-list example
    By Application Development in forum Fortran
    Replies: 0
    Last Post: 06-21-2007, 08:19 AM
  4. linked list
    By Application Development in forum C
    Replies: 0
    Last Post: 06-11-2007, 07:41 AM