A friend of mine said that microsoft asked him this question:
You have a linked list in memory. It either ends in a null pointer, or it loops. It is not a 'true' linked list, as the loop does not necessarily point back to the beginning, but may point to some place in the middle, making a loop partway through. How do you traverse the list without looping on forever (either by hitting the end, or realizing you looped)?
Stipulations:
1. The list may take up nearly all available RAM, so you cannot store list information to track it. (other than a couple of counters, perhaps)
2. The list is singly-linked
3. The list may be modified, but has to be fixed upon the end of traversal.
This is a tough one.
You have a linked list in memory. It either ends in a null pointer, or it loops. It is not a 'true' linked list, as the loop does not necessarily point back to the beginning, but may point to some place in the middle, making a loop partway through. How do you traverse the list without looping on forever (either by hitting the end, or realizing you looped)?
Stipulations:
1. The list may take up nearly all available RAM, so you cannot store list information to track it. (other than a couple of counters, perhaps)
2. The list is singly-linked
3. The list may be modified, but has to be fixed upon the end of traversal.
This is a tough one.