Unit 5 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
head
is None
)?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list starting from the head node, collect the values of each node, and return them in a list.
1) Initialize `current` as the starting node (`head`) of the linked list.
2) Create an empty list `values` to store the values of each node encountered.
3) Traverse through the linked list:
a) Append the value of `current` to the `values` list.
b) Move to the next node by updating `current` to `current.next`.
4) Continue the traversal until `current` is `None`.
5) Return the list `values` containing the values of all nodes in the linked list.
⚠️ Common Mistakes
None
, which could lead to attempts to access attributes of None
.current
properly, leading to an infinite loop or missing nodes.class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def print_linked_list(head):
current = head
values = []
while current:
values.append(current.value)
current = current.next
return values