Singly Linked List in Java

Singly Linked List implementaton in Java

What do you mean by singly linked list in Java programming?

Singly linked list in Java  is  one of the simplest data structure and used for the implementation of advance data structures such as tree and graph. Singly linked list are efficient as compared to arrays because every node of the list have a pointer that points to the address of next node in the list thus most of the operations in the list becomes easy.

Let us see more about singly linked list in Java programming in this article.

Advantages of  Singly Linked List

.

  • Dynamic Data Structure 

        Linked list is a dynamic in nature so it can grow and shrink at runtime by allocating and deallocating memory. 

  • Insertion and Deletion 

          Insertion and Deletion becomes easy in linked list .

  • Memory Wastage 

        As linked list is dynamic in nature we can increase as  well as decrease the size of list thus memory is saved 

  • Implement 

         Linked list are widely used in programming of stacks and queues .

Structure of singly linked list in Java

class Node {  

 int data;  

Node next; 

 Node(int d)



data = d;  

next=null;
 }  
    }  

Operation on singly linked list

  • Deletion 
  • Insertion
  • Traversals

Java code for creating a singly linked list

public class LinkedList  
{  
 Node head;  // head of list  
 static class Node {  
 int data;  
         Node next;  
 Node(int d)  
 { 
     data = d;  next=null
}  
     
    }  
     //print from start to end 
 public void show()  
     {  
         Node n = head;  
 while (n != null)  
         {  
 System.out.print(n.data+” \n);  
             n = n.next;  
         }  
     }  
     
 public static void main(String[] args)  
     {  
         /* Start with the empty list. */  
 LinkedListExamples list = new LinkedListExamples();  
  
 list.head = new Node(300);  
         Node second      = new Node(400);  
         Node third       = new Node(500);  
  
 list.head.next = second; // Link first node–> second node  
 second.next = third; // Link first node –>second node  
 list.show();  
     }  
}  
 

 

OUTPUT:

300

400

500