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?
Be sure that you clarify the input and output parameters of the problem:
Run through a set of example cases:
HAPPY CASE
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
EDGE CASE
Input: head = [0], k = 1
Output: [0]
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.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We still count k
nodes at a time. If we find k
nodes, then we reverse them.
1) find size of list
2) find no. of times we need to reverse the list
3) start reversing it for that "times" for each group of size k
4) make sure to point start.next to the node containing remaining linked list elements
5) return the last node of the first group
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
# move min(k, len(list)) times
cur = head
count = 0
while cur and count < k:
cur = cur.next
count += 1
# this check is needed in case remaining list is shorter than k
# then we want to return head directly (no reversing)
if count == k:
# recursively get the first node of the next k nodes, call it `last`
# after we are done reversing current k, we point last of the current k node to `last`
last = self.reverseKGroup(cur, k)
cur = head
prev = None
# standard reverse linked list except condition is not while node but for count times
# https://www.geeksforgeeks.org/reverse-a-linked-list/ <- animation really helpful
for _ in range(count):
next = cur.next
cur.next = prev
prev = cur
cur = next
head.next = last
return prev
return head
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// find size of list
int n = 0;
ListNode current = head;
while (current != null) {
n++;
current = current.next;
}
// find no. of times we need to reverse the list
int times = n / k;
// start reversing it for that "times" for each group of size k
while (times > 0) {
ListNode returnHead = null;
ListNode start = head;
ListNode startNext = null;
ListNode end = null;
ListNode prev = null;
ListNode cur = head;
ListNode ford = null;
// case when we are reversing the first group of list
// we need to save the last element of this group
// because this will become the head in the returning statement
if (times == n / k) {
for (int i = 0; i < k; i++) {
ford = cur.next;
cur.next = prev;
prev = cur;
cur = ford;
}
returnHead = prev;
}
startNext = cur;
// reverse this group by K elements by basic reversal algorithm
for (int i = 0; i < k; i++) {
ford = cur.next;
cur.next = prev;
prev = cur;
cur = ford;
}
end = prev;
start.next = end;
start = startNext;
times--;
}
// make sure to point start.next to the node containing remaining linked list elements
start.next = cur;
// return the last node of the first group
return returnHead;
}
}
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.