<aside>
💡 Build going outwards, wrapping in every while
call starting from null
</aside>
https://leetcode.com/problems/reverse-linked-list/
function reverseList(head) {
let previous = null;
while (head) {
const next = head.next;
head.next = previous;
previous = head;
head = next;
}
return previous;
}
<aside> 💡 Use set because it can handle objects, node values are not unique.
</aside>
https://leetcode.com/problems/linked-list-cycle/
function hasCycle(head) {
const visited = new Set();
let current = head;
while (current) {
if (visited.has(current)) {
return true;
}
visited.add(current);
current = current.next;
}
return false;
}