Implementing a Linked List in Java Using Class
# Implementing a Linked List in Java using Class
A linked list is a linear data structure where each element is a separate object. Linked lists are often used because of their efficient insertion and deletion. They can be used to implement several other common abstract data types, including lists, stacks, queues, associative arrays, and S-expressions, though it is not uncommon to implement the other data structures directly without using a linked list as the basis.
In this tutorial, we will learn how to implement a linked list in Java using a class. We will be using the Node class to represent each node in the linked list. Each node will contain two parts – data and a reference to the next node. We will also create a LinkedList class to store the head of the list and perform various operations on the list.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
A linked list is a linear data structure that contains a sequence of nodes, where each node consists of data and a pointer to the next node in the list. Implementing a linked list in Java using a class is a straightforward process.
Below is a sample code of a Linked List class in Java:
```
public class LinkedList {
Node head;
//Node class
static class Node{
int data;
Node next;
//Constructor to create a new node
Node(int d){
data = d;
next = null;
}
}
// Method to insert a new node
public static LinkedList insert(LinkedList list, int data){
//Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
//If the Linked List is empty, then make the new node as head
if(list.head == null){
list.head = new_node;
}
else {
//Else traverse till the last node and insert the new_node there
Node last = list.head;
while (last.next != null){
last = last.next;
}
//Insert the new_node at last node
last.next = new_node;
}
//Return the list by head
return list;
}
// Method to print the LinkedList.
public static void printList(LinkedList list){
Node currNode = list.head;
System.out.print("LinkedList: ");
//Traverse through the LinkedList
while (currNode != null){
//Print the data at current node
System.out.print(currNode.data + " ");
//Go to next node
currNode = currNode.next;
}
}
//Main method
public static void main(String[] args) {
//Start with the empty list
LinkedList list = new LinkedList();
//Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
//Print the LinkedList
printList(list);
}
}
```
The above code implements a LinkedList class that can be used to create a linked list data structure. The LinkedList class contains a Node class that is used to create new nodes. The insert method is used to add a new node with the given data to the end of the list. The printList method prints out the data in the linked list. The main method creates a linked list and adds the values 1-8 to it, then prints out the list.