Skip to main content

28 patterns for 14 weeks

Week 1

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Input: nums = [2,7,11,15], target = 9 Output: [0,1]

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> numMap =  new HashMap<>();

        for(int i = 0 ; i < nums.length;i++ ){
           int complement = target-nums[i];
           if(numMap.containsKey(complement)){
            return new int[]{numMap.get(complement),i};
           }
           numMap.put(nums[i],i);

        }
        return null;

    }
}

83. Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Input: head = [1,1,2] Output: [1,2]

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // Store the original head to return it later
        ListNode res = head;

        // Traverse the list while both current node and next node are not null
        while (head != null && head.next != null) {

            // If current node and next node have the same value,
            // skip the next node by pointing current's 'next' to the node after next.
            if (head.val == head.next.val) {
                head.next = head.next.next;
            } else {
                // If values are different, move to the next node
                head = head.next;
            }
        }

        // Return the original head of the updated list
        return res;
    }
}

82. Remove Duplicates from Sorted List II

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

Input: head = [1,2,3,3,4,4,5] Output: [1,2,5]

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // Create a dummy node that points to the head.
        // This helps handle edge cases, such as when the first few nodes are duplicates.
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        // 'prev' will always point to the last node before a group of duplicates.
        ListNode prev = dummy;

        // 'curr' is used to traverse the list from the head.
        ListNode curr = head;

        // Traverse the list until 'curr' and 'curr.next' are both non-null.
        // We need 'curr.next' to compare the current node's value with the next node's value.
        while (curr != null && curr.next != null) {

            // Check if the current node has the same value as the next one — indicates a duplicate.
            if (curr.val == curr.next.val) {
                // Move 'curr' forward until the value changes (i.e., skip all duplicates).
                while (curr.next != null && curr.val == curr.next.val) {
                    curr = curr.next;
                }

                // Now 'curr' points to the last node of the duplicate sequence.
                // Skip all duplicates by linking 'prev.next' to the node after 'curr'.
                prev.next = curr.next;
            } else {
                // If there's no duplication, just move 'prev' forward to 'curr'.
                prev = prev.next;
            }

            // In both cases (whether duplicates were found or not), move 'curr' forward.
            curr = curr.next;
        }

        // Return the next node after dummy, which is the head of the cleaned list.
        return dummy.next;
    }
}

Week 2