Unit 6 Session 1 (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?
HAPPY CASE
Input: head = Node(1, Node(2, Node(3)))
Output: 3 -> 2 -> 1
Explanation: Each node's pointer is reversed, resulting in the tail becoming the new head.
EDGE CASE
Input: head = None
Output: None
Explanation: An empty list remains empty when reversed.
Match what this problem looks like to known categories of problems, e.g. Linked List or Two Pointers, and strategies or patterns in those categories.
The problem of reversing a singly linked list in place is fundamental in manipulating data structures, especially linked lists. There is no special trick to it, we will just need to carefully keep track of our pointers as we go.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Reverse the pointers of the list nodes one by one, moving from the head towards the tail.
1) Initialize a pointer `previous` to `None` and start with `current` at the head of the list.
2) Iterate through the list, for each node:
- Store the next node temporarily.
- Reverse the `current.next` pointer to point to `previous`.
- Move `previous` and `current` forward.
3) Return `previous` as it will be the new head of the reversed list after the loop completes.
Implement the code to solve the algorithm.
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def reverse(head):
previous = None # This will eventually become the new head of the reversed list
current = head # Start with the head of the original list
while current:
next_node = current.next # Temporarily store the next node
current.next = previous # Reverse the current node's pointer
previous = current # Move pointers one position forward
current = next_node # Continue to next node
return previous # At the end, 'previous' will be the new head of the reversed list
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n)
where n
is the length of the linked list. Each node in the list is visited once to reverse its pointer.O(1)
because the space used is constant, involving only a few pointer variables, regardless of the size of the input list.