What is Java LinkedList?

A Linked List is a type of linear data structure. It differs from arrays in that elements are not kept in contiguous locations. Instead, Nodes in a linked list are linked using pointers.

Linked List Creation

Structure of a node

There are two parts to each linked list node:

Source: Node Structure

Here,

Data: Data that get stored at a specific location.

Reference: Provides the address of the following linked list node.

Creation & Insertion Operations

Our goal here is to create a java linked list and insert a new node to the end it. For example, we have a Linked List 1→3→5→7→9 and want to add 11 to the end. Adding 11 to the end of the given linked list will update it to 1→3→5→7→9->11.

Code

public class InsertNode {
    class Node {
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public Node head = null;
    public Node tail = null;

    public void addNode(int data) {
        System.out.println("Adding a new node with value "+data+" at the end of the linked list ");
        Node new_Node = new Node(data);

        if (head == null) {
            head = new_Node;
            tail = new_Node;
        } else {

            tail.next = new_Node;
            tail = new_Node;
        }
    }

    public void PrintData() {

        Node current = head;
        if (head == null) {
            System.out.println("Linked List is empty");
            return;
        }
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        InsertNode List = new InsertNode();

        List.addNode(1);
        List.PrintData();

        List.addNode(3);
        List.PrintData();

        List.addNode(5);
        List.PrintData();

        List.addNode(7);
        List.PrintData();
    }
}


Output

Adding a new node with value 1 at the end of the linked list
1 Adding a new node with value 3 at the end of the linked list
1 3
Adding a new node with value 5 at the end of the linked list
1 3 5
Adding a new node with value 7 at the end of the linked list
1 3 5 7 

write your code here: Coding Playground

Deletion Operations

Here we will learn how to remove the first occurrence of a given node from the linked list, i.e., remove the first occurrence of a given key.

For example, if we have a Linked List 1→3→5→7→9, we want to remove the first occurrence of node 3 (key = 3) from the linked list.

By deleting the first instance of the node with value 3 from the linked list, we will end up with: 1→5→7→9.

Sample Code

public class LinkedList {

    Node head;
    static class Node {

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

    public static LinkedList value(LinkedList list, int data) {

        Node new_node = new Node(data);
        new_node.next = null;

        if (list.head == null) {
            list.head = new_node;
        } else {
            Node last = list.head;
            while (last.next != null) {
                last = last.next;
            }

            last.next = new_node;
        }

        return list;
    }

    public static void printData(LinkedList list) {
        Node current_Node = list.head;

        System.out.print("Linked List :- ");

        while (current_Node != null) {
            System.out.print(current_Node.data + " ");
            current_Node = current_Node.next;
        }
        System.out.println();
    }

    public static LinkedList deleteNode(LinkedList list,int key) {

        Node current_Node = list.head, prev = null;
        if (current_Node != null && current_Node.data == key) {
            list.head = current_Node.next;
            System.out.println(key + " is found and deleted");

            return list;
        }

        while (current_Node != null && current_Node.data != key) {
            prev = current_Node;
            current_Node = current_Node.next;
        }

        if (current_Node != null) {
            prev.next = current_Node.next;
            System.out.println(key + " is found and deleted");
        }

        if (current_Node == null) {
            System.out.println(key + " not found");
        }

        return list;
    }


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

        list = value(list, 1);
        list = value(list, 3);
        list = value(list, 5);
        list = value(list, 7);
        list = value(list, 9);
        list = value(list, 11);
        list = value(list, 13);

        printData(list);
        deleteNode(list, 3);
        printData(list);

        deleteNode(list, 7);
        printData(list);

        deleteNode(list, 14);
        printData(list);
    }
}

Output

Linked List:- 1 3 5 7 9 11 13
3 is found and deleted
Linked List:- 1 5 7 9 11 13
7 is found and deleted
Linked List:- 1 5 9 11 13
14 not found
Linked List:- 1 5 9 11 13 

write your code here: Coding Playground