Skip to content

Instantly share code, notes, and snippets.

public class GoldbachConjecture {
public static void Goldbach(int x) {
if (x % 2 != 0) {
System.out.println("Not Even");
return;
}
if (x <= 2) {
System.out.println("Less than 2");
return;
public class isBST {
public static Node prevNode = null;
// method 1: do inOrder and check if it is in ascending order
// doesnt work in case of duplicates
public boolean isBST1(Node root) {
if (root != null) {
if (!isBST1(root.left))
return false;
if (prevNode != null && prevNode.data >= root.data) {
public class CircularLinkedList {
public int size =0;
public Node head=null;
public Node tail=null;
//add a new node at the start of the linked list
public void addNodeAtStart(int data){
System.out.println("Adding node " + data + " at start");
public class DoublyLinkedList {
int size =0;
Node head = null;
Node tail = null;
public Node addAtStart(int data){
System.out.println("Adding Node " + data + " at the start");
Node n = new Node(data);
if(size==0){
public class NthNodeFromEnd {
//Method 1
//Get the length of the Linked list, say it x
//nth node from the end will be x-n+1 from the start of the linked list
public static int getNodeUsingLength(Node head, int n){
int x =0;
Node curr = head;
while(curr!=null){
x++;
public class DiameterOfTree {
public int getHeight(Node root) {
if (root != null) {
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
return 0;
}
public int Diameter(Node root) {
public class DiameterOfTree {
int diameter = 0;
// here in improved solution, we calculate the height and diameter for every
// node in same iteration
// every Node will return 2 values, diameter and height wrt to the
// particular node
public int[] Diameter(Node root) {
int DandH[] = { 0, 0 }; // initialize the height (DandH[0]) and diameter
// as 0 (DandH[1])
public class StackUsingLinkedList {
Node head= null;
int size =0;
public void push(int data){
Node x = new Node(data);
if(getSize()==0){
head = x;
}else{
//add the Node at the start of a Linked List
import java.util.Hashtable;
public class SimpleHashTable {
int [] a = new int[5];
String [] arrNames = new String[]{"Sumit","Jain","Raghav","Garg","Gaurav","Rishi"};
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
public class RearrageArrayPositiveNegative {
int[] arrA;
public RearrageArrayPositiveNegative(int[] arrA) {
this.arrA = arrA;
}
public void divideGroups(int low, int high) {
if (low >= high)
return;