remove loop from a linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rameshwar83
    New Member
    • Feb 2007
    • 4

    remove loop from a linked list

    Suppose we have a linked list..... haviing loop..... suggest me some algo to remove loop from list.....
    Ex. A->B->C->D->E->F->G->H->I->E in this list node iI points to node E ....
    make this list like this... A->B->C->D->E->F->G->H->I->NULL

    thanks....
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think a better idea would be to not create the loop in the first place.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      I agree. The only algorithm I can think of would be horrendous as far as efficiency is concerned...Loo ping through ever node in the List, check to see if its next pointer is the same as any other node in the List - if so, you have a duplicate - remove the last one.

      Comment

      • vpawizard
        New Member
        • Nov 2006
        • 66

        #4
        Hello,
        A simple technique is to create a flag member in the node to indicate whether node has been visited or not. If the node is visited, the flag is set. If the node is revisited, the flag will be set indicating loop. However, if you want to find the loop, the algorithm will be too expensive.


        Regards,

        Comment

        Working...