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 […]
Category: Java Programs
Delete Node from Singly Linked List in Java
In 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) […]
Java program to reverse Linked List using iterative and recursive approach
In 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 […]
Java program to find duplicates in an array
In 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 : […]
Java program to merge two sorted linked lists
In this article, we will discuss how to merge two sorted linked lists so that the resultant Linked List is also in sorted order. This is a frequently asked interview question. Example : List 1 : 10 -> 30 -> 50 -> 70 List 2 : 20 -> 40 -> 60 -> 80 Merged List […]