Java Program to Implement LinkedList

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Java program to implement LinkedList

Output

LinkedList: 1 2 3 

In the above example, we have implemented the singly linked list in Java. Here, the linked list consists of 3 nodes.

Each node consists of value and next. The value variable represents the value of the node and the next represents the link to the next node.

To learn about the working of LinkedList, visit LinkedList Data Structure.


Example 2: Implement LinkedList using LinkedList class

Java provides a built LinkedList class that can be used to implement a linked list.

Output

LinkedList: [Cat, Dog, Horse]
First Element: Cat 
Last Element: Horse

In the above example, we have used the LinkedList class to implement the linked list in Java. Here, we have used methods provided by the class to add elements and access elements from the linked list.

Notice, we have used the angle brackets (<>) while creating the linked list. It represents that the linked list is of the generic type.