Reverse Linked List

<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;
}

Linked List Cycle

<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;
}

Remove Nth Node From End of List