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: 0->1->1->2
Output: 0->1->2
Input: 1->3->4->4
Output: 1->3->4
EDGE CASE
Input: 1->1->2->3
Output: 1->2->3
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: Store all unique elements in a separate array, sorted. Create a new LinkedList from the unique elements.
1) Create an array of size N
2) Iterate through the LinkedList
a) If the element is not the same as the last added element to the
array, append it
3) Create a dummy head node to compare the current node with next nodes' data
4) Iterate through the array
a) Re-assign โnextโ references of the LinkedList node to the value of the next index in the array
b) The last element's 'next' in the LinkedList should be set to null
General Idea: Use two pointers to set the first instance of a duplicate's 'next' to next unique element.
1) Establish two pointers
2) While the two pointers are not null
a) Re-assign 'next' of each node to a node with a different value
b) Move both pointers to the node with a different value
c) Repeat
3. Return head node
โ ๏ธ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
# Establish two pointers
prev = curr = head
# While two pointers are not null
while curr:
# Re-assign 'next' of each node to a node with a different value
while curr and prev.val == curr.val:
curr = curr.next
prev.next = curr
# Move both pointers to the node with a different value
prev = curr
# Return the head node
return head
class Solution {
public ListNode deleteDuplicates(ListNode head) {
// Establish two pointers
ListNode slow = head;
ListNode fast = head;
// While two pointers are not null
while (fast != null) {
// Re-assign 'next' of each node to a node with a different value
while (fast != null && slow.val == fast.val) {
fast = fast.next;
}
slow.next = fast;
// Move both pointers to the node with a different value
slow = fast;
}
// Return the head node
return head;
}
}
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.
Assume N
represents the number of nodes in the linked list.
O(N)
because we need to traverse all nodes in the linked list.O(1)
because we only need two pointers for memory.