Codepath

Halve List

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Linked Lists, Traversal

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 happens if the linked list is empty?
    • If the linked list is empty, the function should return None.
HAPPY CASE
Input: head = Node(4) -> Node(8) -> Node(12)
Output: Node(2) -> Node(4) -> Node(6)
Explanation: Each value in the linked list is divided by two.

EDGE CASE
Input: head = None
Output: None
Explanation: When the linked list is empty, the function returns None.

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:

  • Traversal of a linked list
  • Modification of node values

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the linked list, modifying each node's value by dividing it by two.

1) Start at the head of the linked list.
2) While the current node is not None, do the following:
    a) Divide the value of the current node by two.
    b) Move to the next node in the list.
3) Return the head of the modified linked list.

⚠️ Common Mistakes

  • Forgetting to move to the next node in the list, causing an infinite loop.
  • Not handling an empty linked list properly.

4: I-mplement

Implement the code to solve the algorithm.

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

# For testing
def print_linked_list(head):
    current = head
    while current:
        print(current.value, end=" -> " if current.next else "\n")
        current = current.next

def halve_list(head):
    current = head
    while current:
        current.value = current.value / 2
        current = current.next
    return head

5: R-eview

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

  • Trace through your code with an input to check for the expected output
  • Catch possible edge cases and off-by-one errors

Example:

node_one = Node(5)
node_two = Node(6)
node_three = Node(7)
node_one.next = node_two
node_two.next = node_three

# Input List: 5 -> 6 -> 7
print_linked_list(halve_list(node_one))

Expected Output: 2.5 -> 3 -> 3.5

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(1) because we are only modifying the values in place without using extra space.
Fork me on GitHub