Two solutions.
Elegant solution:
1. Set the Head node to point to Null
2. Traverse the list, and reverse the links as you go.
3. When you get to the end, if the End ptr is the same as the Head ptr,
you have a loop.
Hack solution:
1. Most current systems are word-aligned, so for each node, you set the
lowest bit to 1 (incrementing the ptr by 1 is wrong).
If you come across a Node that has it already set (dirty bit), you
have a loop.
Both cases have an order of N to solve, with the edge given to the hack.
the elegant solution was thought of by the only junior dev in my company.
the hack by a senior.
(I was busy working of course).
Elegant solution:
1. Set the Head node to point to Null
2. Traverse the list, and reverse the links as you go.
3. When you get to the end, if the End ptr is the same as the Head ptr,
you have a loop.
Hack solution:
1. Most current systems are word-aligned, so for each node, you set the
lowest bit to 1 (incrementing the ptr by 1 is wrong).
If you come across a Node that has it already set (dirty bit), you
have a loop.
Both cases have an order of N to solve, with the edge given to the hack.
the elegant solution was thought of by the only junior dev in my company.
the hack by a senior.
(I was busy working of course).