Java Program to Implement LinkedList

Java Program to Implement LinkedList

What is LinkedList ?

In this Article, We will going to write a Program to Implement LinkedList.

A linked list is a linear data structure in which each element (called a node) stores a reference to the next element in the list. Linked lists are dynamic data structures, which means that their size can change during the execution of a program. In contrast, arrays have a fixed size, which means that you need to specify the size of the array when you create it and you cannot change it later.

Algorithm to Implement LinkedList:

  1. Define a class Node to represent a single node in the list. The Node class should have at least two fields: a data field to store the value of the node, and a next field to store a reference to the next node in the list.
  2. Define a class LinkedList to represent the linked list itself. The LinkedList class should have a reference to the head node of the list.
  3. Define a constructor for the LinkedList class that initializes the head node to null.
  4. Define a method to add a new node to the end of the list. This method should take the new data value as a parameter, create a new Node object to represent the new node, and add it to the end of the list by traversing the list until the last node is reached and then setting its next field to the new node.
  5. Define a method to remove a node from the list. This method should take the value to be removed as a parameter, and then traverse the list looking for the node with that value. If the node is found, it should be removed by updating the next field of the previous node to point to the node after the one being removed.
  6. Define a method to print the contents of the list. This method should traverse the list starting at the head node, printing the value of each node as it goes.
  7. Define a main method to test the LinkedList class. In the main method, create a new LinkedList object, add some nodes to the list, remove some nodes from the list, and print the contents of the list to verify that the implementation is correct.

Program to Implement LinkedList:

Run
import java.util.*;

class Main {
    static class LinkedList {
        Node head;

        static class Node {
            int value;
            Node next;

            Node(int d) {
                value = d;
                next = null;
            }
        }
    }

    public static void display(LinkedList linkedList) {
        System.out.print("LinkedList: ");
        while (linkedList.head != null) {
            System.out.print(linkedList.head.value + " ");
            linkedList.head = linkedList.head.next;
        }
    }

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

        linkedList.head = new LinkedList.Node(1);
        LinkedList.Node second = new LinkedList.Node(2);
        LinkedList.Node third = new LinkedList.Node(3);

        linkedList.head.next = second;
        second.next = third;

        display(linkedList);
    }
}

Output:

LinkedList: 1 2 3

Java Program to implement LinkedList Using LinkedList Class:

Run
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        // create a new LinkedList
        LinkedList list = new LinkedList<>();

        // add some elements to the list
        list.add(1);
        list.add(2);
        list.add(3);

        // print the contents of the list
        System.out.println("Initial list: " + list);

        // remove the second element from the list
        list.remove(1);

        // add a new element at the end of the list
        list.add(4);

        // print the contents of the list again
        System.out.println("Modified list: " + list);
    }
}
Output:

Initial list: [1, 2, 3]
Modified list: [1, 3, 4]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription