Skip to main content

Posts

Showing posts with the label Linked List

Print the Elements of a Linked List Hackerrank Solution - java

 Print the Elements of a Linked List Hackerrank Solution - java For Explanation Watch Video : code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.regex.*; public   class  Solution {      static   class  SinglyLinkedListNode {          public   int  data;          public  SinglyLinkedListNode next;          public  SinglyLinkedListNode( int  nodeData) {              this .data = nodeData;              this .next = null;         }     }      static   class  SinglyLinkedList {          public  SinglyLinkedListNode head;          public  SinglyLinkedListNode tail;          public  SinglyLinkedList() {              this .head = null;              this .tail = null;         }          public   void  insertNode( int  nodeData) {             SinglyLinkedListNode node =  new  SinglyLinkedListNode(nodeData);              if  ( this .head == null) {                  this .head = node;   

Delete Nth node from the end of the given linked list Solution - java

 Delete Nth node from the end of the given linked list Solution . For Explanation Watch Video : Code:: class Solution {     public ListNode removeNthFromEnd(ListNode head, int n) {         int count = 0;//5         ListNode temp1 = head;         while(temp1!=null){             count++;             temp1 = temp1.next;         }         if(count==n){             return head.next;         }         ListNode temp2 = head;         int remove = count - n;//3         int count1 = 0;         while(count1!=remove-1){             temp2 = temp2.next;             count1++;         }         temp2.next = temp2.next.next;         return head;     } }

Find the middle of a given linked list Solution - java

 Find the middle of a given linked list Solution - java For Explanation watch video:  Code:: Function to find Middle element if Head Of Linked List Given class Solution {     int getMiddle(Node head)     {          Node slow = head;          Node fast = head;          while(fast!=null && fast.next!=null){              slow = slow.next;              fast = fast.next.next;          }          return slow.data;     } }