Traverse a circular linked list safely
JavaScriptfunction traverse(tail) {
if (!tail) return;
const head = tail.next;
let current = head;
do {
console.log(current.data);
current = current.next;
} while (current !== head);
}Explanation
The do-while pattern guarantees at least one visit, which is perfect for circular lists, especially single-node lists. The stop condition is reaching head again, not null.
Output
For 10 -> 25 -> 37 -> 52 -> back to 10, the traversal prints 10, 25, 37, 52 exactly once
Real-life Example
A round-robin scheduler keeps moving to the next process forever, and after the last process it naturally returns to the first one.