Singly Linked List in Java

Deep Dive into Singly linked list in Java

Singly Linked List in Java is one of the most powerful data structures there. Let us have a look on how we can write the Java Program for a singly linked List below

java

What is a Singly Linked List in Java?

A singly Linked List in Java is a collection of nodes that are connected in a chained sequence. Where each node has the following –

  • Data Value
  • Next Reference – Reference to the next node in the sequence

Unlike Arrays with are contiguously stored, Linked List is not contiguous and are scattered all over the memory but connected with one another using the next references.

Also, array size can not be dynamically increased however, in Linked List the size can be increased and decreased at any time dynamically.

Singly Linked List in Java

Structure of singly linked list in Java

// Singly Linked List Node Class in Java
class Node{
    int data;
    Node next;

    Node(int val)
    {
        data = val;
        next = null;
    }
}
  • 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 .

Operation on singly linked list

  • Deletion 
  • Insertion
  • Traversals
Example of a Singly Linked List in Java

Method 1 (Linked List in Java)

This method uses Linked List objects to call member functions.

We will perform the following operations –

  • Deletion 
  • Insertion
  • Traversals

Code (Method 2)

Run
// Linked in Java Program
import java.lang.*;

class LinkedList {
    Node head;
    // not using parameterized constructor would by default
    // force head instance to become null
    // Node head = null;  // can also do this, but not required

    // Node Class
    class Node {
        int data;
        Node next;

        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    }

    public Node insertNode(int data) {
        // Using constructor to create memory and value assignment
        Node new_node = new Node(data);
        // current head becomes this new_node's next
        new_node.next = head;

        // changing head to this newly created node
        head = new_node;

        return head;
    }

    public void deleteNode() {
        if (head == null) {
            System.out.println("Not Possible to delete from Empty Singly Linked List");
            return;
        }

        // we will just move head to next node
        head = head.next;
    }


    public void traverse() {
        Node node = head;
        System.out.print("Linked List: ");
        int count = 0;

        //as linked list will end when Node reaches Null
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
            count++;
        }
        System.out.println("\nSingly Linked List currently has " + count + " nodes\n");
    }

}
class Main{

    public static void main(String args[])
    {
        LinkedList listObj = new LinkedList();

        listObj.insertNode(25);
        listObj.insertNode(20);
        listObj.insertNode(15);
        listObj.insertNode(10);
        listObj.insertNode(5);

        listObj.traverse();

        listObj.deleteNode();
        listObj.deleteNode();
        listObj.deleteNode();

        listObj.traverse();

    }
}

Output

Linked List: 5 10 15 20 25 
Singly Linked List currently has 5 nodes

Linked List: 20 25 
Singly Linked List currently has 2 nodes

Method 2 (Linked List in Java)

This method non-member static functions to work with a linked list, the head needs to be passed separately.

We will perform the following operations –

  • Deletion 
  • Insertion
  • Traversals

Code (Method 1)

Run
// Node Class
class Node {
    int data;
    Node next;

    Node(int x) // parameterized constructor
    {
        data = x;
        next = null;
    }
}
class Main{

    // inserts node at the front on Linked List
    public static Node insertNode(Node head, int data) {
        // Using constructor to create memory and value assignment
        Node new_node = new Node(data);
        // current head becomes this new_node's next
        new_node.next = head;

        // changing head to this newly created node
        head = new_node;

        return head;
    }

    // deletes node from the front of Linked List
    public static Node deleteNode(Node head) {
        if (head == null) {
            System.out.println("Not Possible to delete from Empty Singly Linked List");
            return head;
        }

        // we will just move head to next node
        head = head.next;
        return head;
    }


    public static void traverse(Node node) {
        System.out.print("Linked List: ");
        int count = 0;

        //as linked list will end when Node reaches Null
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
            count++;
        }
        System.out.println("\nSingly Linked List currently has " + count + " nodes\n");
    }


    public static void main(String args[])
    {
        Node head = null;

        head = insertNode(head, 25);
        head = insertNode(head, 20);
        head = insertNode(head, 15);
        head = insertNode(head, 10);
        head = insertNode(head, 5);

        traverse(head);

        head = deleteNode(head);
        head = deleteNode(head);
        head = deleteNode(head);

        traverse(head);

    }
}

Output

Linked List: 5 10 15 20 25 
Singly Linked List currently has 5 nodes

Linked List: 20 25 
Singly Linked List currently has 2 nodes

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java