Java Program to Add elements to a LinkedList

Java Program to Add elements to a LinkedList

What is 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.

There are various ways to Add elements to a LinkedList :

1.Using the add() method: This method adds an element to the end of the list.

LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);

2.Using the addFirst() method: This method adds an element to the beginning of the list.

LinkedList list = new LinkedList();
list.addFirst(1);
list.addFirst(2);
list.addFirst(3);

3.Using the addLast() method: This method also adds an element to the end of the list.

LinkedList list = new LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);

4.Using the add() method with an index: This method adds an element at the specified index in the list.

LinkedList list = new LinkedList();
list.add(0, 1);
list.add(1, 2);
list.add(2, 3);

5.Using the offer() method: This method also adds an element to the end of the list.

LinkedList list = new LinkedList();
list.offer(1);
list.offer(2);
list.offer(3);

6.Using the offerFirst() method: This method adds an element to the beginning of the list.

LinkedList list = new LinkedList();
list.offerFirst(1);
list.offerFirst(2);
list.offerFirst(3);

7.Using the offerLast() method: This method also adds an element to the end of the list.

LinkedList list = new LinkedList();
list.offerLast(1);
list.offerLast(2);
list.offerLast(3);

8.Using the push() method: This method also adds an element to the beginning of the list.

LinkedList list = new LinkedList();
list.push(1);
list.push(2);
list.push(3);

Example 1 :  

Run

import java.util.LinkedList;

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

        // Using the add() method
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println("Using the add() method: " + list);

        // Using the addFirst() method
        list.addFirst(0);
        System.out.println("Using the addFirst() method: " + list);

        // Using the addLast() method
        list.addLast(4);
        System.out.println("Using the addLast() method: " + list);

        // Using the add() method with an index
        list.add(2, 5);
        System.out.println("Using the add() method with an index: " + list);
    }
}

Output :

Using the add() method: [1, 2, 3]
Using the addFirst() method: [0, 1, 2, 3]
Using the addLast() method: [0, 1, 2, 3, 4]
Using the add() method with an index: [0, 1, 5, 2, 3, 4]

Explanation :

This code demonstrates how to add elements to a linked list in Java using the add(), addFirst(), addLast(), and add() methods with an index.

  1. The add() method is used to add an element to the end of the list. In this code, the elements 1, 2, and 3 are added to the list using the add() method.
  2. The addFirst() method is used to add an element to the beginning of the list. In this code, the element 0 is added to the beginning of the list using the addFirst() method.
  3. The addLast() method is used to add an element to the end of the list. In this code, the element 4 is added to the end of the list using the addLast() method.
  4. The add() method with an index is used to add an element at a specific position in the list. In this code, the element 5 is added at index 2 in the list using the add() method with an index.

Example 2:  

Run

import java.util.LinkedList;

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

        // Using the offer() method
        list.offer(1);
        list.offer(2);
        list.offer(3);
        System.out.println("Using the offer() method: " + list);

        // Using the offerFirst() method
        list.offerFirst(0);
        System.out.println("Using the offerFirst() method: " + list);

        // Using the offerLast() method
        list.offerLast(4);
        System.out.println("Using the offerLast() method: " + list);

        // Using the push() method
        list.push(5);
        System.out.println("Using the push() method: " + list);
    }
}

Output :

Using the offer() method: [1, 2, 3]
Using the offerFirst() method: [0, 1, 2, 3]
Using the offerLast() method: [0, 1, 2, 3, 4]
Using the push() method: [5, 0, 1, 2, 3, 4]

Explanation : 

This code demonstrates how to add elements to a linked list in Java using the offer(), offerFirst(), offerLast() and push() method.

  1. The offer() method is used to add an element to the end of the list. In this code, the elements 1, 2, and 3 are added to the list using the offer() method.
  2. The offerFirst() method is used to add an element to the beginning of the list. In this code, the element 0 is added to the beginning of the list using the offerFirst() method.
  3. The offerLast() method is used to add an element to the end of the list. In this code, the element 4 is added to the end of the list using the offerLast() method.
  4. The push() method is used to add an element to the beginning of the list. In this code, the element 5 is added to the beginning of the list using the push() method.

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