Reversing a string is a popular problem that is often encountered during technical interviews or daily coding practice. In this post, we’ll discuss two different ways to reverse a string in Java: using an iterative approach and using built-in functions. 1. Iterative approach public class ReverseString { public static void main(String[] args) { String original …
Continue reading How to Reverse a String in Java (Two Approaches)Java Programs
In this article, we will see Java algorithm to find out if a String is a permutation of a palindrome. Palindrome is a word or phrase that is the same when you traverse from beginning or end. For example , “race car”, “anna”, “abcba” are palindromes. Now, the String “car race” is a permutation …
Continue reading Java program to check if a String is permutation of a PalindromeIn this article, we will see Java program to delete Node from Singly Linked List. To delete a node, we have to find its previous node and set prev.next = current.next as shown below. private Node delete(Node head, int val) { Node n = head; if (n.data == val) return n.next; while (n.next != null) …
Continue reading Delete Node from Singly Linked List in JavaIn this article, we will see Java program to reverse Linked List using iterative and recursive approach. Iterative approach Steps : Declare previous, current and next nodes. Until current node is null, do this : next = current.next current.next = previous previous = current current = next return the previous node private Node reverseIterative(Node …
Continue reading Java program to reverse Linked List using iterative and recursive approachIn this article, we will see algorithms to find duplicates in an array. package com.topjavatutorial; import java.util.HashSet; import java.util.Set; public class ArrayDemo { public static void main(String[] args) { int[] arr = { 10, 20, 20, 30 }; System.out.println(containDups(arr)); } private static boolean containDups(int[] x) { Set<Integer> d = new HashSet<Integer>(); for (int i : …
Continue reading Java program to find duplicates in an array