Fundamentals/Patterns/Linked List — Dummy + Predecessor2 problems· Linked List

Use dummy head + curr-as-predecessor pattern; check curr.next, mutate or advance

When to useDelete nodes, insert nodes, any operation that needs the node BEFORE the target

iterative
Deleting Nodes
deleting_nodes
Delete All Occurrences from Linked List
delete_all_occurrences
INITIALIZE
dummy = { val: 0, next: head }; curr = dummy
const dummy = { val: 0, next: head };
let curr = dummy;
const dummy = { val: 0, next: head };
let curr = dummy;
TRAVERSE
while (curr.next !== null)
while (curr.next !== null) {
while (curr.next !== null) {
[CHECK]
if curr.next satisfies condition
if (curr.next.val === target) {
if (curr.next.val === value) {
splice
curr.next = curr.next.next
curr.next = curr.next.next;
curr.next = curr.next.next;
advance
else curr = curr.next
curr = curr.next;
curr = curr.next;
RETURN
return dummy.next
return dummy.next;
return dummy.next;