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 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
Subscribe to:
Posts (Atom)