Unit 5 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
original
value is not found in any node?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list from the head, checking each node's value and replacing it if it matches the original
value.
1) Start at the `head` of the linked list.
2) Traverse through the list using a loop, checking each node's value.
3) If a node's value matches `original`, update it to `replacement`.
4) Continue until all nodes have been checked (i.e., until the end of the list).
⚠️ Common Mistakes
head
is None
).def ll_replace(head, original, replacement):
current = head
while current:
if current.value == original:
current.value = replacement
current = current.next