Posts

Showing posts with the label Data Structures

Next Greater Element Geeks For Geeks And InterviewBit

 Approach 1: Using ArrayList                  ArrayList<Long> aa=new ArrayList<Long>();         Stack<Long>stack=new Stack<Long>();         for(int i=n-1;i>-1;i--){             if(stack.isEmpty()==true){                 aa.add(Long.valueOf(-1));             }             else{                 if(stack.peek()>arr[i]){                     aa.add(stack.peek());                 }                 else{                     while(!stack.isEmpty() && stack.peek()<=arr[i]){               ...

Print in Reverse HackerRank

  public   static   void  reversePrint(SinglyLinkedListNode head) {             Stack <Integer> stack= new  Stack<Integer>();             SinglyLinkedListNode temp=head;              while (temp!=null){                 stack.push(temp.data);                 temp=temp.next;                                         }             while (!stack.isEmpty...

Reverse A Linked List HackerRank

  public   static  SinglyLinkedListNode reverse(SinglyLinkedListNode llist) {             SinglyLinkedListNode temp=llist;             Stack<Integer> stack= new  Stack<Integer>();              while (temp!=null){                 stack.push(temp.data);                 temp=temp.next;             }             temp=llist;              while (!stack.isEmpty()){           ...

Delete a Node HackerRank

public   static  SinglyLinkedListNode deleteNode(SinglyLinkedListNode head,  int  pos)  {     SinglyLinkedListNode temp=head;         SinglyLinkedListNode old;          int  i= 0 ;          if (pos> 0 ){          while (i<pos- 1 ){             temp=temp.next;             i++;         }              old=temp.next.next;         // temp.next.next=null;          temp.next=old;           return  head;   ...

Insert a node at a specific position in a linked list HackerRank

  public   static  SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head,  int  data,  int  pos) {         SinglyLinkedListNode temp=head;         SinglyLinkedListNode old;         SinglyLinkedListNode newNode= new  SinglyLinkedListNode(data);          int  i= 0 ;          while (i<pos- 1 ){             temp=temp.next;             i++;         }         old=temp.next;         temp.next=newNode;         newNode.next=old; ...

Get Node Value HackerRank Problem Solving

  public   static   int  getNode(SinglyLinkedListNode head,  int  pos) {            SinglyLinkedListNode temp=head;              int  count= 0 ;              while (temp!=null){                 count++;                 temp=temp.next;                              }              int  d=count-pos- 1 ;              int  i= 0 ;    ...

Geeks For Geeks Reverse A Linked List

 class Solution {     //Function to reverse a linked list.     Node reverseList(Node head)     {         Stack<Integer> stack=new Stack<Integer>(); Node temp=head; while(temp!=null){ stack.push(temp.data);             temp=temp.next; } temp=head;         while(!stack.isEmpty()){             temp.data=stack.pop(); //System.out.println("Data is: "+temp.data);             temp=temp.next;         }// code here         return head;     } }

COMPARE TWO LINKED LIST HACKKERANK

  static   boolean  compareLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {        SinglyLinkedListNode temp1=head1;        SinglyLinkedListNode temp2=head2;         int  count1= 0 ;         int  count2= 0 ;         int  m= 0 ;         int  count3= 0 ;                 while (temp1!=null){                        count1++;            temp1=temp1.next;        }          /*while(temp2!=null){  ...

DAY 15 LINKED LIST HACKKERANK

    public   static   Node insert(Node head, int  data) {         Node newNode= new  Node(data);          Node temp=head;          if (head==null){             head=newNode;              return  head;         }          else {              while (temp.next!=null){                 temp=temp.next;             }             temp.next=newNode;  ...

2D Array - DS HACKKERANK

  import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'hourglass sum function below.      *      * The function is expected to return an INTEGER.      * The function accepts 2D_INTEGER_ARRAY arr as a parameter.      */          //Time O(1) as we know every time we have to iterate the loop //through value 4 as array size w...

Arrays DS HACKKERANK

  public   static  List<Integer> reverseArray(List<Integer> a) { Approach 1 Time O(N) Space O(N)        List<Integer> aa= new  ArrayList<Integer>();         for ( int  i= 0 ;i<a.size();i++){            aa.add(a.get(i));        }         Collections.reverse(aa);          return  aa;     } /* Given is the list so we have two choices 1.either to use arrays for storing data 2.either to use ArrayList for storing data The thing is we need to return the List in the end so we can use directly the ArrayList. */ Approach 2 Time O(N) Space O(1)     Collections.reverse(a);     return a;

LINKED LIST IN JAVA (IMPLEMENTATION WITH INSERTION,DELETION AND UPDATION IN START, END AND MIDDLE)

 import java.util.Scanner; class NodeImplemetation{ Node head; int count=0; int count2=0; public void toAdd(int data){ Node newNode=new Node(data); Node temp=head; if(isEmpty()){ System.out.println("Initially List is empty. "); head=newNode; return; } while(temp.next!=null){ temp=temp.next; } temp.next=newNode; } public void toAddMid(int data,int count){ Node temp=head; int index=0; while(index<count/2){ temp=temp.next; index++; } Node old=temp.next; Node newNodeMid= new Node(data); temp.next=newNodeMid; newNodeMid.next=old; } public void toInsertStart(int data){ Node newStart=new Node(data); Node temp=head; newStart.next=temp; head=newStart; } public int countTotal(){ Node temp=head; while(temp!=null){ //System.out.println("Data is : "+temp.data); temp=temp.next; count++; } return coun...