How to implement your own Linked List In Java w.r.t competitive coding

As you already know that Java provides us with its own LinkedList class and its various helper methods but what if you want to implement your own linked list class? With your own custom methods? Let’s see how you can achieve that.

Let’s create a C, C++ type linked list, and we know that in a normal linked list, we want a data field to hold our data and a next field to point to the next node in the linked list. So, the basic structure of our node will look like this –

class Node {
	int data; // to hold our data value
	Node next; // to point to next node in the linked list

	// constructor
	Node(int data) {
		this.data = data;
	}
}

Well, now we can break our problem into three problem sets –

  • Creation of the first node in Linked List.
  • Creation of all other nodes in Linked List ( here we will add a node to the end of a linked list).
  • Finally, Iteration over the linked list.

For fullfilling above three conditions, we will need –

  • A head object ( to mark the starting of the linked list).
  • A lastNodeInLinkedList object ( to keep track of the last node in the linked list).
  • An iterator (to iterate over the linked list).

So, without further ado, let’s move on to the code part of this –

package factorial;

import java.util.Scanner;

class Node {
	int data; // to hold our data value
	Node next; // to point to next node in the linked list

	// constructor
	Node(int data) {
		this.data = data;
	}
}

public class Codekru {

	public static void main(String[] args) {
		Node head = null;
		Node lastNodeInLinkedList = null;

		Scanner sc = new Scanner(System.in); // to take input
		int n = sc.nextInt(); // the number of elements in the linked list

		for (int i = 0; i < n; i++) {
			int data = sc.nextInt(); // data elements of the linked list

			// this "if" block will be executed only at the start of the linked list
			if (head == null) {
				Node newNode = new Node(data); // adding a new node to linked list
				newNode.next = null;
				head = newNode; // making this as head node
				lastNodeInLinkedList = newNode; // marking current node as last node in the list
			}
			// this will be executed while adding nodes to the linked list
			else {
				Node newNode = new Node(data); // adding a new node to linked list
				newNode.next = null;
				lastNodeInLinkedList.next = newNode; // connecting the lastNodeInLinkedList to newly created node
				lastNodeInLinkedList = newNode; // marking current node as last node in the list
			}
		}

		printLinkedList(head);

	}

	// printing the linked list
	public static void printLinkedList(Node head) {
		Node iterator = head;
		System.out.print("Linked list: ");
		while (iterator != null) {
			System.out.print(iterator.data + " ");
			iterator = iterator.next;
		}
	}

}

Input

5 1 2 3 4 5

Output

Linked list: 1 2 3 4 5 

Well in this article, we have learned how to make our own linked list and how to iterate over it. Hope you find this helpful 🙂

Please feel free to write us in comments or mail us at [email protected] for any queries or concerns.

Liked the article? Share this on

Leave a Comment

Your email address will not be published.