Unit 5 Session 2 (Click for link to problem statements)
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
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?
None
.HAPPY CASE
Input: head = Node(5) -> Node(6) -> Node(7)
Output: Node(6) -> Node(7) -> Node(8)
Explanation: Each node's value in the linked list is incremented by 1.
EDGE CASE
Input: head = Node(0)
Output: Node(1)
Explanation: When the linked list contains only one node, its value is incremented by 1.
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:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list, incrementing the value of each node by 1.
1) Start at the head of the linked list.
2) While the current node is not `None`, do the following:
a) Increment the value of the current node by 1.
b) Move to the next node.
3) Return the head of the modified linked list.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def increment_ll(head):
current = head
while current:
current.value += 1
current = current.next
return head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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(increment_ll(node_one))
# Expected Output: 6 -> 7 -> 8
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(N) because we need to traverse all nodes in the linked list.
- Space Complexity: O(1) because we are modifying the list in place and not using extra space.