CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2004
    Posts
    1

    circular singly linked list problem

    hi all ,
    this problem was given to me to solve.

    given a pointer to a node in a singly linked list how do u find out whether the list is circular or not?
    any solutions other than traversing the whole list.
    thanks
    sayan

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    you can save the adresses of the first and last nodes in the list as class members, and compere them.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    I think that the first and last nodes are not the same nodes in a circular list. What happens is that the last node "points" to the first node of the list. In normal lists (not circular) the last node points to NULL.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    Originally posted by yiannakop
    I think that the first and last nodes are not the same nodes in a circular list. What happens is that the last node "points" to the first node of the list.
    since it's circuler .. this makes no difference
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Mar 2004
    Location
    Temuco, CHILE
    Posts
    161

    Re: circular singly linked list problem

    If you don't know where the last pointer on (last.next) points to, the only way is to traverse the whole list- This is by definition.

    If you know the last pointer points to a specific node (which is assumed to be either the header or the first node of the list), you spend less time simply traversing from that node. You expect to reach your current node in less time that you would spend going forwards to reach the end of the list and then go back to the begginning.

    But, as I said, the only way to know for sure, is to iterate through it. No matter what is the implementation (singled, doubled; with extra nodes for the beg or end, etc), if you reach your node _twice_ fron anywhere (once from itself), the list _is_ circular.

  6. #6
    Join Date
    Jul 2007
    Posts
    2

    Thumbs up Re: circular singly linked list problem

    I think following steps will solve your problem.
    1 ] ptr1 = current given prointer;
    2 ] ptr2 = current given pointer;
    3]
    do
    {
    pt1 = ptr1 -> next;
    ptr2 = ptr2 -> next-> next;
    }
    while (ptr2 != NULL && ptr1 != ptr2)

    4] If you find ptr2 as a NULL then linked list is not circular
    and if you find ptr1== ptr2 then linked list has loop.

  7. #7
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: circular singly linked list problem

    Quote Originally Posted by N0Em0t10n
    I think following steps will solve your problem.
    1 ] ptr1 = current given prointer;
    2 ] ptr2 = current given pointer;
    3]
    do
    {
    pt1 = ptr1 -> next;
    ptr2 = ptr2 -> next-> next;
    }
    while (ptr2 != NULL && ptr1 != ptr2)

    4] If you find ptr2 as a NULL then linked list is not circular
    and if you find ptr1== ptr2 then linked list has loop.
    will this still work if the loop is of size 3 nodes?

  8. #8
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: circular singly linked list problem

    Use the two pointer approach.

    Rough code:

    Code:
    bool ContainsLoop (Node *pNode)
    {
         Node *pSlowptr = pNode;
         Node *pFastptr = pNode->pNext;
         while (pFastptr != NULL || pSlowptr != NULL)
         {
                if (pSlowptr == pFastptr)
                       return true;
    
                pSlowptr = pSlowptr->pNext;
                if (pFastptr->pNext)
                       pFastptr = pFastptr->pNext->pNext;
                else
                       pFastptr = NULL;
         }  
         return false;
    Hope it helps.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  9. #9
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: circular singly linked list problem

    Quote Originally Posted by ka3ak
    will this still work if the loop is of size 3 nodes?
    my bad, misread the question.

  10. #10
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: circular singly linked list problem

    Is there any proof that two pointer approach will work..???

    I mean any mathematical proof or something...

  11. #11
    Join Date
    Jul 2007
    Posts
    36

    Re: circular singly linked list problem

    Is a proof that neccessary? I mean, all its doing is having one pointer go through the list one at a time, the second pointer will go through the list at double the rate.

    If the list isn't circular, the second pointer will be null. Answer 1 given, not circular.
    Else the second point will loop through the list once or twice dependant on the amount of elements in the list. Twice if n is even, once if n is odd.
    (Note: O(n), because this algorithm takes n steps.)

    The two are inevitable to meet based on taking it as factors, they will come to a common meeting point.


    Another way to approach this is save the starting point, run through the entire list untill null or the starting point is found. Based on which one is the result you would know if its circular or not. It would still be O(n) so there wouldn't be any benefit, just another option.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured