adsaaaa
adsaaaa
2. Reverse an Array
arr[left++] = arr[right];
arr[right--] = temp;
}
3. Find Kth Smallest/Largest Element in an Array
import java.util.PriorityQueue;
maxHeap.offer(num);
return maxHeap.peek();
switch (arr[mid]) {
int index = 0;
class Node {
int val;
Node next;
current.next = prev;
prev = current;
current = next;
return prev;
slow = slow.next;
fast = fast.next.next;
}
return false;
slow = slow.next;
fast = fast.next.next;
return slow;
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
tail = tail.next;
return dummy.next;
}
10. Remove Nth Node from End of List
dummy.next = head;
first = first.next;
second = second.next;
second.next = second.next.next;
return dummy.next;
class StackArray {
top = -1;
stack[++top] = value;
}
public int pop() {
return stack[top--];
return stack[top];
class StackLinkedList {
int data;
Node next;
Node(int data) {
this.data = data;
newNode.next = top;
top = newNode;
top = top.next;
return value;
return top.data;
import java.util.Stack;
stack.push(ch);
} else if (ch == ')' || ch == '}' || ch == ']') {
return stack.isEmpty();
if (Character.isDigit(ch)) {
stack.push(ch - '0');
} else {
int b = stack.pop();
int a = stack.pop();
switch (ch) {
return stack.pop();
stack.push(arr[i]);
return result;
class QueueArray {
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int value) {
queue[rear] = value;
size++;
size--;
return value;
return queue[front];
return size == 0;
class QueueLinkedList {
Node next;
Node(int data) {
this.data = data;
rear = newNode;
front = front.next;
return value;
return front.data;
}
public boolean isEmpty() {
class CircularQueue {
front = 0;
rear = -1;
size = 0;
queue[rear] = value;
size++;
return value;
return queue[front];
return queue[rear];
return size == 0;
import java.util.LinkedList;
import java.util.Queue;
queue.offer("1");
System.out.println(binary);
queue.offer(binary + "0");
queue.offer(binary + "1");
import java.util.Stack;
class QueueUsingStacks {
stack1.push(value);
if (stack2.isEmpty()) {
return stack2.pop();
class BinaryTree {
int data;
Node(int data) {
this.data = data;
Node root;
if (root == null) {
return root;
inorderTraversal(root);
if (node != null) {
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);
if (root != null) {
inorderTraversal(root.left);
inorderTraversal(root.right);
if (root != null) {
preorderTraversal(root.left);
preorderTraversal(root.right);
if (root != null) {
postorderTraversal(root.left);
postorderTraversal(root.right);
System.out.print(root.data + " ");
import java.util.LinkedList;
import java.util.Queue;
queue.offer(root);
while (!queue.isEmpty()) {
class TreeDiameter {
static class Result {
int diameter;
heightAndDiameter(root, result);
return result.diameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Graph {
public Graph() {
adjacencyList.get(src).add(dest);
import java.util.*;
queue.offer(start);
visited.add(start);
while (!queue.isEmpty()) {
if (!visited.contains(neighbor)) {
queue.offer(neighbor);
visited.add(neighbor);
visited.add(current);
public static boolean hasCycle(Graph graph, int vertex, int parent, Set<Integer> visited) {
visited.add(vertex);
if (!visited.contains(neighbor)) {
return true;
return false;
if (!visited.contains(vertex)) {
return false;
}
int count = 0;
if (!visited.contains(vertex)) {
count++;
return count;
import java.util.*;
this.src = src;
this.dest = dest;
this.weight = weight;
class DisjointSet {
parent.put(vertex, vertex);
if (parent.get(vertex) != vertex) {
parent.put(vertex, find(parent.get(vertex)));
return parent.get(vertex);
Collections.sort(edges);
if (root1 != root2) {
mst.add(edge);
ds.union(edge.src, edge.dest);
return mst;
int n = graph.length;
Arrays.fill(key, Integer.MAX_VALUE);
key[0] = 0;
int result = 0;
int u = -1;
inMST[u] = true;
result += key[u];
key[v] = graph[u][v];
return result;
if (n <= 1) return n;
dp[0] = 0;
dp[1] = 1;
return dp[n];
}
38. Climbing Stairs
if (n <= 2) return n;
dp[1] = 1;
dp[2] = 2;
return dp[n];
int n = cost.length;
return dp[n];
if (nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
dp[0] = nums[0];