Linked lists and node identity
Remove nodes by changing links while retaining surviving objects
A pending-job queue stores linked nodes, and other parts of the app hold references to particular jobs. A customer category is disabled, so all matching jobs must leave the queue. Rebuilding a fresh chain with the same surviving values would leave those references pointing at the old objects.
This is why node identity matters. The task is to reconnect the existing survivors, including when cancelled jobs appear consecutively or at the head.
Removing cancelled jobs without replacing survivors
An array filter preserves values and order by building a new array. A linked-list operation can have a stronger contract: preserve the actual surviving node objects. Reconstructing equivalent nodes produces the right printed values but fails identity checks held by other callers.
JavaScript objects have reference identity. Two new Node(3) objects are different even though both contain 3. Use strict equality to reason about which node survives.
Reconnecting through the predecessor
Each node contains a value and a next reference. To remove it, change the incoming link to its successor rather than allocating a replacement survivor. Removing the head needs special treatment or a temporary sentinel whose next points at the old head. That sentinel may be new; the result's surviving nodes may not be.
A previous-survivor pointer must advance only when the current node survives. Advancing it across a removed node can strand the retained chain. Preserve original survivor order and terminate the result at null; accidentally pointing backward creates a cycle.
Convert blocked values into a Set for membership checks without changing the caller's array or Set. Garbage collection reclaims unreachable removed nodes, but it does not fix incorrect links or preserve identity for reconstructed nodes. Outside references may keep removed nodes alive; their existence is independent of whether the result includes them.
import { Node } from "./exercise.mjs";
const original = new Node(3);
const alias = original;
const replacement = new Node(3);
console.log(alias === original, replacement === original); // true falseWhat the links do not make cheap
Filtering with repeated blocked-array searches costs O(n b). A blocked Set permits expected O(n+b) time. Relinking needs constant pointer state beyond the membership set; returned arrays require their own output space. Linked lists are useful when stable node identity or local link changes matter, but array locality often wins for ordinary scans.
Other components can keep their references to surviving jobs because those nodes were never replaced.
Practice Linked lists and node identity
Implement the exercise in a full-screen workspace, then run its tests.