-
Notifications
You must be signed in to change notification settings - Fork 243
Increment Linked List Node Values
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: 5-10 mins
- 🛠️ Topics: Linked Lists, Traversal
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
- What happens if all the values in the linked list are already at their maximum integer value?
- This is not a concern in Python due to its handling of large integers, but it's worth considering in other languages.
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:
- Traversal of a linked list
- In-place modification of node values
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.
- Forgetting to handle the case where the linked list is empty.
- Not correctly updating the values, leading to incorrect outputs.
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.
- Ensure that each node's value has been incremented correctly by printing the linked list after running the function.
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.