March 13, 2024

Java Problem Solution: Implement a Linked List in Java


public class LinkedList {
    public static void main(String[] args) {
        // Test LinkedList
        Node head = createSampleList();
        System.out.print("Original Linked List: ");
        printLinkedList(head);

        int newData = 123; // Insert new node anywhere
        head = insertAnywhere(head, newData, 2);
        System.out.println("Linked List after inserting " + newData + " at position 5: ");
        printLinkedList(head);

        int dataToDelete = 4; // Delete node with data 4
        head = deleteNode(head, dataToDelete);
        System.out.println("Linked List after deleting node with data " + dataToDelete + ": ");
        printLinkedList(head);
    }
    static class Node {
        int data;
        Node next;
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    // Function to create a sample LinkedList
    private static Node createSampleList() {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        return head;
    }
    // Function to print the elements of a LinkedList
    private static void printLinkedList(Node head) {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    // Function to insert a new node at any position in the LinkedList
    private static Node insertAnywhere(Node head, int newData, int position) {
        Node newNode = new Node(newData);
        if (head == null || position <= 0) {
            newNode.next = head;
            return newNode;
        }
        Node current = head;
        for (int i = 0; i < position - 1 && current.next != null; i++) {
            current = current.next;
        }
        newNode.next = current.next;
        current.next = newNode;
        return head;
    }
    // Function to delete a node with a given data value from the LinkedList
    private static Node deleteNode(Node head, int dataToDelete) {
        if (head == null) {
            return null;
        }
        if (head.data == dataToDelete) {
            return head.next;
        }
        Node current = head;
        while (current.next != null) {
            if (current.next.data == dataToDelete) {
                current.next = current.next.next;
                return head;
            }
            current = current.next;
        }
        return head;
    }
}

March 12, 2024

Java Problem Solution: Find the Intersection of two Arrays in Java


import java.util.Arrays;
public class ArrayIntersection {
    public static void main(String[] args) {
        int[] arr1 = {1, 8, 3, 4, 5};
        int[] arr2 = {4, 5, 6, 7, 8, 5, 1};
        int[] intersection = findIntersection(arr1, arr2);
        System.out.print("Intersection of arrays: ");
        for (int num : intersection) {
            System.out.print(num + " ");
        }
    }
    public static int[] findIntersection(int[] arr1, int[] arr2) {
        int len1 = arr1.length;
        int len2 = arr2.length;
        int minLength = Math.min(len1, len2);
        int maxLength = Math.max(len1, len2);
        int[] result = new int[minLength];
        int index = 0;
        // Determine the smaller and larger arrays
        int[] smallerArray = (len1 <= len2) ? arr1 : arr2;
        int[] largerArray = (len1 > len2) ? arr1 : arr2;
        for (int num : smallerArray) {
            if (contains(num, largerArray)) {
                result[index++] = num;
            }
        }
        return Arrays.copyOf(result, index);
    }
    public static boolean contains(int value, int[] arr) {
        for (int num : arr) {
            if (num == value) {
                return true;
            }
        }
        return false;
    }
}

March 11, 2024

Java Problem Solution: Check if a Number is Prime or Not in Java


public class PrimeChecker {
    public static void main(String[] args) {
        int numberToCheck = 4; // Change this to test different numbers.
        if (isPrime(numberToCheck)) {
            System.out.println(numberToCheck + " is a prime number.");
        } else {
            System.out.println(numberToCheck + " is not a prime number.");
        }
    }
    // A prime number is a natural number greater than 1 that has no positive divisors
    // other than 1 and itself.
    public static boolean isPrime(int number) {
    	//return true if the number is prime, false otherwise.
    	if (number <= 1) {
            return false; // Numbers less than or equal to 1 are not prime
        }
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false; // If number is divisible by any other number, it's not prime
            }
        }
        return true; // If the loop completes without finding divisors, number is prime
    }
}

Want 1 to 1 personalized Java training? Email me at isingh30 AT gmail please. View my following video