DEV Community

Cover image for Creating of LinkedList
Samanvi Thota
Samanvi Thota

Posted on • Edited on

2

Creating of LinkedList

The LinkedList class of the Java collections framework provides the functionality of the linked list data structure (doubly linkedlist).

Each element in a linked list is known as a node. It consists of 3 fields:
Prev - stores an address of the previous element in the list. It is null for the first element
Next - stores an address of the next element in the list. It is null for the last element
Data - stores the actual data

  • Syntax for creating a LinkedList:
LinkedList<Type> linkedList = new LinkedList<>(); 
Enter fullscreen mode Exit fullscreen mode

Here, Type indicates the type of a linked list. For example,

// create Integer type linked list

LinkedList<Integer> linkedList = new LinkedList<>();

// create String type linked list

LinkedList<String> linkedList = new LinkedList<>(); 
Enter fullscreen mode Exit fullscreen mode
  • Example Program to create LinkedList:
import java.util.LinkedList;

class Main {
  public static void main(String[] args){

    // create linkedlist
    LinkedList<String> animals = new LinkedList<>();

    // Add elements to LinkedList
    animals.add("Dog");
    animals.add("Cat");
    animals.add("Parrot");
    System.out.println("LinkedList: " + animals);
  }
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

LinkedList: [Dog, Cat, Parrot]
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay