Problem Statement: Write a Java program to create a custom LinkedList class with methods to add elements, remove elements, find the length, and reverse the list.
Here’s a custom linked list class in Java with methods to add elements, remove elements, find the length, and reverse the list.
package com.javacodepoint.collection;
class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
public class CustomLinkedList<T> {
private Node<T> head;
private int size;
public CustomLinkedList() {
head = null;
size = 0;
}
// Method to add an element to the end of the linked list
public void add(T data) {
Node<T> newNode = new Node<>(data);
if (head == null) {
head = newNode;
} else {
Node<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
size++;
}
// Method to remove an element from the linked list
public void remove(T data) {
if (head == null) {
return;
}
if (head.data.equals(data)) {
head = head.next;
size--;
return;
}
Node<T> current = head;
Node<T> prev = null;
while (current != null && !current.data.equals(data)) {
prev = current;
current = current.next;
}
if (current != null) {
prev.next = current.next;
size--;
}
}
// Method to find the length of the linked list
public int length() {
return size;
}
// Method to reverse the linked list
public void reverse() {
if (head == null || head.next == null) {
return;
}
Node<T> prev = null;
Node<T> current = head;
Node<T> next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
// Method to print the linked list
public void print() {
Node<T> current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
CustomLinkedList<Integer> myList = new CustomLinkedList<>();
myList.add(1);
myList.add(2);
myList.add(3);
myList.add(4);
myList.add(5);
System.out.println("Original Linked List:");
myList.print();
System.out.println("Length of the Linked List: " + myList.length());
myList.remove(3);
System.out.println("Linked List after removing 3:");
myList.print();
myList.reverse();
System.out.println("Reversed Linked List:");
myList.print();
}
}
OUTPUT:
Original Linked List:
1 -> 2 -> 3 -> 4 -> 5 -> null
Length of the Linked List: 5
Linked List after removing 3:
1 -> 2 -> 4 -> 5 -> null
Reversed Linked List:
5 -> 4 -> 2 -> 1 -> null
Explanation:
Node
class: This class represents a single node of the linked list. Each node contains data of typeT
and a reference to the next node.CustomLinkedList
class: This is the main custom linked list class that contains the methods for adding, removing, finding the length, and reversing the linked list.add(T data)
: Adds an element to the end of the linked list by traversing the list until the last node and appending the new node there.remove(T data)
: Removes the first occurrence of the specified element from the linked list. It updates thehead
if the element to remove is the first node and traverses the list to find and remove the element otherwise.length()
: Returns the length of the linked list.reverse()
: Reverses the linked list by iteratively changing thenext
pointers of each node.print()
: Prints the elements of the linked list in their order.
main
method: In themain
method, we create an instance of theCustomLinkedList
class, add elements, demonstrate the methods, and print the linked list before and after various operations.
This custom linked list class provides basic functionality to work with a singly linked list, and you can extend it further as needed for your specific use cases.
See also: ArrayList Manipulation: add, remove Elements, find size