Implementing A Linked List in Java Using Class: Become A ML Engineer
Implementing A Linked List in Java Using Class: Become A ML Engineer
Implementing A Linked List in Java Using Class: Become A ML Engineer
in
es
Implementing a Linked List in Java using Class
Pre-requisite: Linked List Data Structure
Custom Search
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the
contiguous location, the elements are linked using pointers as shown below.
in
Hire with us!
Become a ML Engineer
Get certi ed from IIT Madras
OPEN
In Java, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class
contains a reference of Node class type.
class LinkedList {
Node head; // head of list
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 1/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
In this article, insertion in the list is done at the end, that is the new node is added after the last node of
the given Linked List. For example, if the given Linked List is 5->10->15->20->25 and 30 is to be inserted,
then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head pointer of it, it is required to traverse the list till the
last node and then change the next of last node to new node.
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 2/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
System.out.print("LinkedList: ");
// Go to next node
⇣
currNode = currNode.next;
}
}
// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
//
// ******INSERTION******
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 3/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
}
}
Output:
LinkedList: 1 2 3 4 5 6 7 8
Traversal
For traversal, below is a general purpose function printList() that prints any given list by traversing the list
from head node to the last.
import java.io.*;
int data;
Node next;
// Constructor
⇣
Node(int d)
{
data = d;
next = null;
}
}
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
}
// **************MAIN METHOD**************
//
// ******INSERTION******
//
Output:
LinkedList: 1 2 3 4 5 6 7 8 ▲
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 5/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
Deletion By KEY
To be done:
Given a ‘key’, delete the rst occurrence of this key in linked list.
How to do it:
To delete a node from linked list, do following steps.
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 6/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
⇣
{
data = d;
next = null;
}
}
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 7/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 8/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
//
// CASE 3: The key is not present
//
// **************MAIN METHOD**************
⇣
//
// ******INSERTION******
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 9/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
Output:
LinkedList: 1 2 3 4 5 6 7 8
1 found and deleted
LinkedList: 2 3 4 5 6 7 8
4 found and deleted
LinkedList: 2 3 5 6 7 8
10 not found
LinkedList: 2 3 5 6 7 8
Deletion At Position
To be done:
Given a ‘position’, delete the node at this position from the linked list.
How to do it:
The steps to do it are as follows:
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 10/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
Case 2: The position is greater than 0 but less than the size of the list, i.e. in the middle or
last, except at head
1. In this case, Find previous node of the node to be deleted.
2. Change the next of previous node to the next node of current node.
3. Free the memory of replaced node.
Case 3: The position is greater than the size of the list, i.e. position not found in the list
1. In this case, No operation needs to be done.
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
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;
}
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
//
// CASE 1:
// If index is 0, then head node itself is to be deleted
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 12/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
// CASE 2:
// If the index is greater than 0 but less than the size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
// Since the currNode is the required position
// Unlink currNode from linked list
prev.next = currNode.next;
// **************MAIN METHOD**************
//
// ******INSERTION******
//
▲
// Insert the values
list = insert(list, 1);
list = insert(list, 2);
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 13/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
//
// ******DELETION AT POSITION******
//
Output:
LinkedList: 1 2 3 4 5 6 7 8
0 position element deleted
LinkedList: 2 3 4 5 6 7 8
2 position element deleted
LinkedList: 2 3 5 6 7 8
10 position element not found
LinkedList: 2 3 5 6 7 8
All Operations
▲
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
// **************INSERTION**************
// **************TRAVERSAL**************
System.out.print("\nLinkedList: ");
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 15/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 16/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
//
// CASE 3: The key is not present
//
// **************DELETION AT A POSITION**************
//
// CASE 1:
// If index is 0, then head node itself is to be deleted
//
// CASE 2:
// If the index is greater than 0 but less than the size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
// Since the currNode is the required position
// Unlink currNode from linked list
prev.next = currNode.next;
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 17/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
else {
// If current position is not the index
// continue to next node
prev = currNode;
currNode = currNode.next;
counter++;
}
}
// **************MAIN METHOD**************
//
// ******INSERTION******
//
//
// ******DELETION BY KEY******
//
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 18/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
//
// ******DELETION AT POSITION******
//
Output:
LinkedList: 1 2 3 4 5 6 7 8
LinkedList: 2 3 4 5 6 7 8
▲
LinkedList: 2 3 5 6 7 8
10 not found
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 19/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
LinkedList: 2 3 5 6 7 8
LinkedList: 3 5 6 7 8
LinkedList: 3 5 7 8
LinkedList: 3 5 7 8
Recommended Posts:
Implementing Iterator pattern of a single Linked List
Create new linked list from two given linked list with greater element at each node
How to add element at rst and last position of linked list in Java?
Find the middle of a given linked list in C and Java
Java Program for Reverse a linked list
Merge a linked list into another linked list at alternate positions
Difference between Singly linked list and Doubly linked list
XOR Linked List - A Memory E cient Doubly Linked List | Set 1
XOR Linked List – A Memory E cient Doubly Linked List | Set 2
Convert singly linked list into circular linked list
Check if a linked list is Circular Linked List ▲
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 20/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
RishabhPrabhu
Technical Content Engineer at GeeksForGeeks
If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected]. See your article
appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you nd anything incorrect by clicking on the "Improve Article" button below.
Article Tags : Data Structures Java Java Programs Linked List Java-Class and Object Java-List-Programs
Practice Tags : Data Structures Java-Class and Object Linked List Java
9
1.8
To-do Done
Based on 6 vote(s)
▲
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 21/22
12/26/2019 Implementing a Linked List in Java using Class - GeeksforGeeks
Please write to us at [email protected] to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/ 22/22