Skip to main content

Posts

Showing posts with the label Linked List DSA

Midpoint of Linked List

/*     Following is the Node class already written for the Linked List     class Node<T> {         T data;         Node<T> next;              public Node(T data) {             this.data = data;         }     } */ public   class  Solution {           public   static  Node<Integer> midPoint(Node<Integer> head) {          //Your code goes here          if (head == null){     ...

how to reverse a LinkedList

/*     Following is the Node class already written for the Linked List     class Node<T> {         T data;         Node<T> next;              public Node(T data) {             this.data = data;         }     } */ public   class  Solution {                                                         ...

remove consicutive duplicate from LinkedList

  /*     Following is the Node class already written for the Linked List     class Node<T> {         T data;         Node<T> next;              public Node(T data) {             this.data = data;         }     } */ public   class  Solution {      public   static  Node<Integer> removeDuplicates(Node<Integer> head) {          //Your code goes here          if (head == null){       ...

Count Nodes of Linked List .

  Problem statement Send feedback Given the head of a singly linked list of integers, find and return its length. public   static  int length(Node head){          while(head != null{               count++;               head = head.next; } } }

Find a Node in Linked List

Question:  Problem statement Send feedback You have been given a singly linked list of integers. Write a function that returns the index/position of integer data denoted by 'N' (if it exists). Return -1 otherwise. Note : Assume that the Indexing for the singly linked list always starts from 0.   /****************************************************************     Following is the class structure of the Node class:     class Node<T> {         T data;         Node<T> next;              public Node(T data) {             this.data = data;         }     } ************************************...

Print ith index on Linked List node from scratch

import java.sql.SQLOutput ; import java.util. * ; public class ithIndexPrint { public static void print ( Node < Integer > head , int i ){ while ( head != null && i > 0 ){ head = head . next ; i -- ; if ( i == 0 ){ System . out .println( head . data ) ; } } } public static Node < Integer > takeInput (){ Scanner scan = new Scanner( System . in ) ; Node < Integer > head = null ; Node < Integer > tail = null ; System . out .println( "Enter Node:" ) ; int data = 0 ; while ( data != - 1 ){ //tab tak ham data lenge jab tk -1 na aa jaye data = scan .nextInt() ; // loop me aane ke bad ham data input lenge Node < Integer > newData = new Node< Integer >( data ) ; // us data ka ek node banayenge if ( head == null ){ //agar head hamara blank hai to usme node ka d...

Print ith Node on LinkList

  /*     Following is the Node class already written for the Linked List     class Node<T> {         T data;         Node<T> next;              public Node(T data) {             this.data = data;         }     } */ public   class  Solution {      public   static  void printIthNode(Node<Integer> head, int i){          while (head != null && i >  0 ){ //jab tak head null nhi ho jata ya i badha nhi hai 0 se tab tak loop chalana hai ...

Linked List Data Structure

Question: How to create without generic Int type Node ? Ans:  public class Node { // this is Node class without Generic int data ; // this is for data like array Element Node next ; //Node ek class hai , usi class ka khud ka variable hai, This is Node(Class) Type variable for //Node is basically refer to class , this is for next element Node ( int data ){ // this is constructor bcse user will pass data value and int because we want to create int type data constructor this . data = data ; // this is refer data next = null ; } }  

Introduction to Arraylists

Question: What is Arraylists and why to use Arraylist ? Ans: As you know, an array is one data structure we have been using till now. A major drawback of an array is that we have to specify its size. However, in scenarios where we need an array that can dynamically adjust or increase in size, we can use an ArrayList. if u want to use Arraylist, we have to import arraylist package, just like scanner  import java.util.ArrayList ; //this is arraylist public class ArrayListDemo { } Array list just like a Generic class of oops,  Java me Generic Class ek class hoti hai jise aap ek ya multiple data types ke saath use kar sakte hain. Generic classes allow you to create classes that can work with different types without sacrificing type safety. In other words, a generic class can be parameterized with different data types.   import java.util.ArrayList ; public class ArrayListDemo { public static void main ( String [] args ){ ArrayList < Integer > list1 = new Array...

LinkedList 1 Data Structure

Question: What are Data Structures? Ans:  Data structures are specialized formats for organizing and storing data in a computer's memory or storage. They define the way data is organized, stored, and manipulated to efficiently perform specific operations. Different data structures are suitable for different tasks, and choosing the right data structure can significantly impact the efficiency and performance of algorithms and programs. See, data is a central part of whatever system we are building, This is a central part right ? Here are some common types of data structures: Arrays: A collection of elements identified by index or key. Linked Lists: A sequence of elements where each element points to the next element in the sequence. Stacks: A Last In, First Out (LIFO) data structure where elements are added and removed from the same end. Queues: A First In, First Out (FIFO) data structure where elements are added at the rear and removed from the front. Trees: Hierarchical data s...