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.
head
), that node is both the head and the tail, so we return its value.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list starting from the head until reaching the last node, and return its value as the tail.
1) Check if the list is empty (`head` is `None`). If it is, return `None`.
2) Initialize a variable `current` to `head` to start traversal.
3) Use a loop to navigate through the list until reaching the last node (where `current.next` is `None`).
4) Return the value of `current` as the value of the tail node.
⚠️ Common Mistakes
None
.def get_tail(head):
if not head:
return None
current = head
while current.next:
current = current.next
return current.value