How to add items to the Linked List in Java. – IT Blog
IT Blog
Boris IT Expert

How to add items to the Linked List in Java.

Boris~October 23, 2020 /Linked List

In this article, you will know how to add item to the linked list java. If you are using the LinkedList type taken from the Java collections, you have several options:

  1. Adding the element at the beginning of the Linked List
linkedList.addFirst(1);
how to add item in the beginning of the linked list in java

2. Adding the element at the end of the Linked List

linkedList.addLast(15);
how to add item at the end of the linked list in java

3. Adding the element in needed place by index

linkedList.add(3, 45); 
// On item with index 3 we will put value 45
how to add item by index in the linked list in java

How to add items to the linked list in java without collections?

If you have implemented the Linked List by yourself. I have explained it in this article. Or you want to know in details how it works because it is one of the questions on the interview; here are some examples:

  1. Adding the element before the first element (Picture will be the same as number 1 from the previous example)
 private void putValueInTheBeginningOfTheLinkedList() {
//You need to create linked list first
        creatingLinkedList();

//Creating new node
        ListNode newNode = new ListNode(999);

//Connecting it on the left side of the head of the linked list. Before the first element
        newNode.next = head;

//Moving head to the beginning of the list
        head = newNode;

//Going back to the start to go through the list for logging
        ListNode selectedItem = head;

//Going through the list until last item don't have connection
        while (selectedItem != null) {
            Log.d("linkedList", "-> " + selectedItem.data);
            selectedItem = selectedItem.next;
        }
    }

2. Adding the element after the last element. (Picture will be the same as number 1 from the previous example)

 private void putValutInTheEndOfTheLinkedList() {
//You need to create linked list first      
        creatingLinkedList();

        //Creating item to put it in the list
        ListNode newNode = new ListNode(999);

        //Finding the first item from the list
        ListNode selectedItem = head;

//Moving selected item to the right until last item don't have a connection
        while (selectedItem.next != null) {
            selectedItem = selectedItem.next;
        }

        //We are on the last item. Connecting it to the new item
        selectedItem.next = newNode;

        //Going back to the start to go through the list for logging
        selectedItem = head;

        //Going through the list until last item don't have connection
        while (selectedItem != null) {
            Log.d("linkedList", "-> " + selectedItem.data);
            selectedItem = selectedItem.next;
        }
    }

In this article, you figured out how to add item to the linked list java in two ways. With the help of the Java collections and in case of implementation via constructor and model class.

Useful links:

Leave Any Questions Related To This Article Below!