-
Notifications
You must be signed in to change notification settings - Fork 237
Find Minimum in Linked List
Unit 5 Session 2 (Click for link to problem statements)
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Finding Minimum
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
.
- If the linked list is empty, the function should return
HAPPY CASE
Input: head = Node(5) -> Node(6) -> Node(7) -> Node(8)
Output: 5
Explanation: The smallest value in the linked list is 5.
EDGE CASE
Input: head = None
Output: None
Explanation: When the linked list is empty, the function returns None.
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
- Comparison of node values
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list, comparing each node's value to find the minimum.
1) If the head is `None`, return `None`.
2) Initialize `min_value` to the value of the head node.
3) Traverse the linked list starting from the second node.
4) If the current node's value is less than `min_value`, update `min_value`.
5) Continue until all nodes have been visited.
6) Return `min_value`.
- Forgetting to handle the case where the linked list is empty.
- Not correctly updating the minimum value as you traverse the list.
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 find_min(head):
if head is None:
return None
min_value = head.value
current = head.next
while current:
if current.value < min_value:
min_value = current.value
current = current.next
return min_value
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:
head1 = Node(5, Node(6, Node(7, Node(8))))
# Linked List: 5 -> 6 -> 7 -> 8
print(find_min(head1)) # Expected Output: 5
head2 = Node(8, Node(5, Node(6, Node(7))))
# Linked List: 8 -> 5 -> 6 -> 7
print(find_min(head2)) # Expected Output: 5
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 using a constant amount of extra space to store the minimum value.