Posts

Showing posts with the label Linked List

Next Greater Node In Linked List LeetCode

 class Solution {     public int[] nextLargerNodes(ListNode head) {         ArrayList<Integer> aa=new ArrayList<Integer>();         Stack<Integer> stack=new Stack<Integer>();                           ListNode temp=head;                 while(temp!=null){             aa.add(temp.val);             temp=temp.next;         }         Collections.reverse(aa);                  System.out.println(aa);                  int ab[]=new int[aa.size()];                  for(int i=0;i<aa.size();i++){             if(stack.isEmpty()){      ...

Remove Duplicate Element from sorted Linked List Geeks For Geeks

 class GfG {     //Function to remove duplicates from sorted linked list.     Node removeDuplicates(Node head)     {     Node temp=head;     Node newNode;/*created in manner it wil help in reference adjustments*/     while(temp.next!=null){         int a=temp.data;         int b=temp.next.data;         if(a==b){            newNode=temp.next.next;            temp.next.next=null;            temp.next=newNode;                      }         else{             temp=temp.next;         }     }     return head;     } } Approach:  Here we will take a node named temp which will point to the node h...

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;  ...

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...