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.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list of cards starting from a given card, collecting all cards until no more cards are linked (i.e., next
is None
).
1) Start with the provided `starting_card`.
2) Initialize an empty list `cards` to hold the cards encountered.
3) Use a while loop to traverse the linked cards:
a) Append the current card to the `cards` list.
b) Move to the next card by updating `current` to `current.next`.
4) Continue until `current` is `None` (indicating the end of the hand).
5) Return the list `cards`.
⚠️ Common Mistakes
current
card to the next in the loop, which would cause an infinite loop.def print_deck(starting_card):
cards = []
current = starting_card
while current:
cards.append(current)
current = current.next
return cards