LinkedList in Java

In this article, we will learn about LinkedList in Java.

The linkedList is part of the collection framework included in the java.util.package. This class is an implementation of the LinkedList data structure, which is a linear data structure in which items are not stored in contiguous locations and each item is an isolated object with a data part and an address part. Items are linked via pointers and addresses. Each element is called a node. They prefer tables because of their dynamics and ease of insertion and deletion. It also has some drawbacks as the nodes cannot be accessed directly, instead, we have to start at the head and follow the link to get to a node that we want to access.

Example

import java.util.*;

public class Test {

	public static void main(String args[])
	{
		// Creating object of the class linked list
		LinkedList<String> ll
			= new LinkedList<String>();

		// Adding elements to the linked list
		ll.add("H");
		ll.add("A");
		ll.addLast("N");
		ll.addFirst("D");
		ll.add(2, "Y");

		System.out.println(ll);

		ll.remove("A");
		ll.remove(3);
		ll.removeFirst();
		ll.removeLast();

		System.out.println(ll);
	}
}

Output:

[D, H, Y, A, N]
[H]

Operations on LinkedList

  1. Adding elements: To add an element to an ArrayList, we can use the add () method. This method is overload to perform multiple operations based on different parameters. They are:
  • add (Object): This method is used to add an element to the end of the LinkedList.
  • add (int index, Object): This method is used to add an element to a specific index in the LinkedList.
import java.util.*;
	
public class AddLinkedList{
	
	public static void main(String args[])
	{
		LinkedList<String> ll = new LinkedList<>();
	
		ll.add("Handy");
		ll.add("Article");
		ll.add(1, "Opinion");
	
		System.out.println(ll);
	}
}

Output:

[Handy, Opinion, Article]
  1. Changing the elements: If after adding the elements we want to change the element, we can do it with the set () method. Since LinkedList is an index, the element we want to change is the reference by the element’s index. Therefore, this method uses an index and the update item to insert at that index.
import java.util.*;
	
public class changeLinkedList{
	
	public static void main(String args[])
	{
		LinkedList<String> ll = new LinkedList<>();
	
		ll.add("Handy");
		ll.add("Opinion");
		ll.add(1, "Handy");
	
		System.out.println("Initial LinkedList " + ll);
	
		ll.set(1, "Article");
	
		System.out.println("Updated LinkedList " + ll);
	}
}

Output:

Initial LinkedList [Handy, Opinion, Handy]
Updated LinkedList [Handy, Handy, Article]

That’s all, we will learn about LinkedList in Java.

Next Article

1. PriorityQueue in Java
2. Stack Class in Java
3. Queue Interface In Java

Please share this post:
Posts created 68

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: