All Codes Java
All Codes Java
import java.util.*;
class Node{
int d;
Node next;
public Node(int data){
d=data;
}
}
public class Main{
public static Node mergeklists(List<Node> lists){
if(lists==null || lists.isEmpty()){
return null;
}
PriorityQueue<Node> minHeap = new PriorityQueue<>((a,b)->a.d-b.d);
for(Node list : lists){
if(list!=null){
minHeap.offer(list);
}
}
Node dummy=new Node(0);
Node current=dummy;
while(!minHeap.isEmpty()){
Node minnode=minHeap.poll();
current.next=minnode;
current=current.next;
if(minnode.next!=null){
minHeap.offer(minnode.next);
}
}
return dummy.next;
}
private static void printLinkedList(Node head){
while(head!=null){
System.out.println(head.d+" ");
head=head.next;
}
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
List<Node> lists=new ArrayList<>();
for(int i=0;i<k;i++){
int size=sc.nextInt();
Node head=new Node(sc.nextInt());
Node current=head;
for(int j=1;j<size;j++){
current.next=new Node(sc.nextInt());
current=current.next;
}
lists.add(head);
}
Node mergedlist = mergeklists(lists);
printLinkedList(mergedlist);
sc.close();
}
}
import java.util.*;
class Node{
int data;
Node next;
Node(int d){
data=d;
next=null;
}
}
class LinkedList{
Node head;
void add(int data){
Node new_node=new Node(data);
if(head==null){
head=new_node;
return;
}
Node current=head;
while(current.next!=null){
current=current.next;
}
current.next=new_node;
}
}
class Solution{
static Node merge(Node head1,Node head2){
if(head1==null){
return head2;
}
if(head2==null){
return head1;
}
Node ans;
if(head1.data<head2.data){
ans=head1;
head1=head1.next;
}
else{
ans=head2;
head2=head2.next;
}
Node temp=ans;
while(head1!=null && head2!=null){
if(head1.data<head2.data){
temp.next=head1;
head1=head1.next;
}
else{
temp.next=head2;
head2=head2.next;
}
temp=temp.next;
}
if(head1!=null){
temp.next=head1;
}
if(head2!=null){
temp.next=head2;
}
return ans;
}
}
class Main
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
LinkedList l1=new LinkedList();
for(int i=0;i<n;i++){
l1.add(sc.nextInt());
}
int m=sc.nextInt();
LinkedList l2=new LinkedList();
for(int i=0;i<m;i++){
l2.add(sc.nextInt());
}
Solution we=new Solution();
Node head=we.merge(l1.head,l2.head);
while(head!=null){
System.out.print(head.data+" ");
head=head.next;
}
}
}
3)PALINDROME OF A LIST
import java.util.*;
import java.lang.*;
import java.io.*;
size++;
}
while(current!=null){
nextNode = current.next;
current.next = prevNode;
prevNode = current;
current = nextNode;
}
return prevNode;
}
if(flag)
System.out.println("true");
else
System.out.println("false");
}
sList.isPalindrome();
}
}
4)REVERSE K ELEMENTS
import java.util.*;
import java.lang.*;
import java.io.*;
5)REORDER LIST
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.*;
class ListNode {
int val;
ListNode next;
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode evenHead = null, evenTail = null;
ListNode oddHead = null, oddTail = null;
if (evenTail != null) {
evenTail.next = oddHead;
} else {
evenHead = oddHead;
}
if (oddTail != null) {
oddTail.next = null;
}
return evenHead;
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
Stack<Integer> st = new Stack<>();
int max = 0;
st.push(-1);
if (st.isEmpty()) {
st.push(i);
}
else{
int len = i - st.peek();
max = Math.max(max, len);
}
}
System.out.println(max);
}
}
9)INFIX TO POSTFIX
import java.util.*;
class Main
{
public static boolean isOperator(char ch){
return ch=='+' || ch=='-' || ch=='*' || ch=='/';
}
public static int getPrecedence(char operator){
switch (operator){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
private static String infixToPostfix(String infix){
StringBuilder postfix=new StringBuilder();
Stack<Character> operatorStack=new Stack<>();
for(char ch:infix.toCharArray()){
if(Character.isLetterOrDigit(ch)){
postfix.append(ch);
}
else if(ch=='('){
operatorStack.push(ch);
}
else if(ch==')'){
while(!operatorStack.isEmpty() && operatorStack.peek()!='('){
postfix.append(operatorStack.pop());
}
operatorStack.pop();
}
else if(isOperator(ch)){
while(!operatorStack.isEmpty() &&
getPrecedence(ch)<=getPrecedence(operatorStack.peek())){
postfix.append(operatorStack.pop());
}
operatorStack.push(ch);
}
}
while(!operatorStack.isEmpty()){
postfix.append(operatorStack.pop());
}
return postfix.toString();
}
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
String infix1=sc.nextLine();
String postfix1=infixToPostfix(infix1);
System.out.println(postfix1);
sc.close();
}
}
import java.util.*;
public class Main {
public static int evaluatePostfix(String[] expressionTokens) {
Stack<Integer> stack = new Stack<>();
for (String token : expressionTokens) {
if (token.matches("-?\\d+")) {
stack.push(Integer.parseInt(token));
} else {
int operand2 = stack.pop();
int operand1 = stack.pop();
switch (token) {
case "+":
stack.push(operand1 + operand2);
break;
case "-":
stack.push(operand1 - operand2);
break;
case "*":
stack.push(operand1 * operand2);
break;
case "/":
stack.push(operand1 / operand2);
break;
}
}
}
return stack.pop();
}
11)BASIC CALCULATOR
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stack<Integer> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
String input = scanner.nextLine();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
int num = c - '0';
while (i + 1 < input.length() && Character.isDigit(input.charAt(i + 1))) {
num = num * 10 + (input.charAt(i + 1) - '0');
i++;
}
operands.push(num);
}
else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (!operators.isEmpty() && precedence(c) <= precedence(operators.peek())) {
evaluate(operands, operators);
}
operators.push(c);
}
}
while (!operators.isEmpty()) {
evaluate(operands, operators);
}
System.out.println(operands.pop());
}
private static int precedence(char c) {
if (c == '+' || c == '-') {
return 1;
}
else if (c == '*' || c == '/') {
return 2;
}
else {
return 0;
}
}
private static void evaluate(Stack<Integer> operands, Stack<Character> operators) {
int b = operands.pop();
int a = operands.pop();
char op = operators.pop();
int result;
if (op == '+') {
result = a + b;
}
else if (op == '-') {
result = a - b;
}
else if (op == '*') {
result = a * b;
}
else {
result = a / b;
}
operands.push(result);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
Main()
{
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
void push(int x)
{
if(!q1.isEmpty())
q1.offer(x);
else
q2.offer(x);
}
int pop()
{
if(q1.isEmpty() && q2.isEmpty())
return -1;
if(!q1.isEmpty())
{
while(q1.size()>1)
q2.offer(q1.poll());
return q1.poll();
}
else
{
while(q2.size()>1)
q1.offer(q2.poll());
return q2.poll();
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
void enqueue(int e)
{
s1.push(e);
}
int dequeue()
{
if(s2.isEmpty())
while(!s1.isEmpty())
s2.push(s1.pop());
if(!s2.isEmpty())
return s2.pop();
else
return -1;
}
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
class BinaryTreeRightSideView {
public static void rightSideView(TreeNode root) {
if (root == null) {
return;
}
while (!queue.isEmpty()) {
int levelSize = queue.size();
if (current.left != null) {
queue.offer(current.left);
}
if (current.right != null) {
queue.offer(current.right);
}
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
}
A a=new A();
height(root,a);
return a.ans;
}
}
class Main
{
public static Node buidTree(String str){
if(str.length()==0 || str.charAt(0)=='N'){
return null;
}
String ip[] =str.split(" ");
Node root=new Node(Integer.parseInt(ip[0]));
Queue<Node> queue= new LinkedList<>();
queue.add(root);
int i=1;
while(queue.size()>0 && i<ip.length){
Node currNode=queue.peek();
queue.remove();
String currVal=ip[i];
if(!currVal.equals("N")){
currNode.left=new Node(Integer.parseInt(currVal));
queue.add(currNode.left);
}
i++;
if(i>ip.length)break;
currVal=ip[i];
if(!currVal.equals("N")){
currNode.right=new Node(Integer.parseInt(currVal));
queue.add(currNode.right);
}
i++;
}
return root;
}
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1=br.readLine();
Node root1=buidTree(s1);
Solution g=new Solution();
System.out.println(g.diameter(root1));
}
}