Print Players Linked List - codepath/compsci_guides GitHub Wiki

TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Linked List, String Manipulation

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the input to the function?
    • The head of a linked list.
  • What is the output of the function?
    • A string of linked list values separated by "->".
HAPPY CASE
Input: A linked list with nodes `isabelle -> saharah -> cj`
Output: `"Isabelle -> Saharah -> C.J."`
Explanation: The values of the nodes are concatenated with the `"->"` separator.

EDGE CASE
Input: An empty linked list.
Output: `"`
Explanation: There are no values to concatenate, so the result is an empty string.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Linked List problems, we want to consider the following approaches:

  • Traversing the list
  • Collecting values
  • String manipulation

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We need to traverse the linked list, collect the values of each node, and concatenate them with the "->" separator.

1) Initialize an empty list to hold the node values.
2) Set the current node to the head of the list.
3) While the current node is not None:
    a) Append the value of the current node to the list of values.
    b) Move to the next node.
4) Join the list of values with the `"->"` separator.
5) Return the resulting string.

⚠️ Common Mistakes

  • Forgetting to handle the case where the linked list is empty.
  • Incorrectly concatenating the values, resulting in extra separators or missing values.

4: I-mplement

Implement the code to solve the algorithm.

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

def print_list(head):
    values = []
    current = head
    while current:
        values.append(str(current.value))
        current = current.next
    return " -> ".join(values)

# Example Usage:
isabelle = Node("Isabelle")
saharah = Node("Saharah")
cj = Node("C.J.")

isabelle.next = saharah
saharah.next = cj

print(print_list(isabelle))  # Output: "Isabelle -> Saharah -> C.J."

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Initial list: isabelle -> saharah -> cj
  • Collected values: ["Isabelle", "Saharah", "C.J."]
  • Final output: "Isabelle -> Saharah -> C.J."

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the number of nodes in the linked list.

  • Time Complexity: O(N) because we need to traverse all the nodes in the linked list.
  • Space Complexity: O(N) because we store the values of all nodes in a list.