LinkedLists – Java Collections Framework

Hello guys, I am back with the 2nd article in my Java Collections Framework article series. Today, I am going to talk about LinkedLists in Java.

What are LinkedLists?

LinkedLists are linear data structures just like arrays. The difference with Linkedlists is that each of the elements in a Linkedlist is connected with the next element using pointers. To elaborate further, each element in the LinkedList has the reference to the next element in the LinkedList .

LinkedList class implements both the Queue interface and List interface in Java.

Let me explain the concept using this well-crafted representation of a LinkedList from BeginnersBook.

LinkedList representation

Let me list down the things you should know about LinkedLists.

  • Elements in a LinkedList are called nodes.
  • Head of a LinkedList contains the address to the first element in the list
  • The Last node of the LinkedList does not contain an address.
  • Each node has a content and the address to the next node except for the last node which only has the content.
  • These types of LinkedLists are called singly-linked lists.

There are also doubly-linked lists which are pretty much the same thing except for that the nodes of doubly-linked lists contain the address of the previous node in addition to other parts.

Features of LinkedList

1. Dynamic Memory Allocation

We don’t have to worry about defining a size to the LinkedList. The compiler does it for you while the program is being executed.

2. They don’t need adjacent memory locations

Because nodes contain the memory address of the next node, the nodes don’t have to be exactly positioned in adjacent locations like in arrays.

Creating a LinkedList

1. First Way
LinkedList<String> myLinkedList = new LinkedList<String>();
2. Second Way
LinkedList<String> myLinkedList = new LinkedList<>();

Adding elements to a LinkedList

        //Adding an element to the end of the list
        myLinkedList.add("Mahela");
        myLinkedList.add("Sanga");
        myLinkedList.add("Angie");

        //Adding an element to the first position
        myLinkedList.addFirst("First Guy");

        //Adding an element to the last position
        myLinkedList.addLast("Last Guy");

        //Adding an element to the 4th position
        myLinkedList.add(3, "Kusal");

Here is the console output of the code snippet.

[First Guy, Mahela, Sanga, Kusal, Angie, Last Guy]

Removing elements from a LinkedList

        //Adding an element to the end of the list
        myLinkedList.add("Thirimanne");
        myLinkedList.add("Mahela");
        myLinkedList.add("Sanga");
        myLinkedList.add("Angie");
        myLinkedList.add("Mahela");
        myLinkedList.add("Johnson");
        myLinkedList.add("Mavan");
        myLinkedList.add("Sanath");
        myLinkedList.add("Mahela");
        myLinkedList.add("Sanga");

        //Removes the first element in the list
        myLinkedList.remove();

        System.out.println("After remove() - " + myLinkedList);

        //Removes the first occurrence of the list
        myLinkedList.removeFirstOccurrence("Mahela");

        System.out.println("After removeFirstOccurrence - " + myLinkedList);

        //Removes the last occurrence of the list
        myLinkedList.removeLastOccurrence("Sanga");

        System.out.println("After removeLastOccurrence - " + myLinkedList);

Here is the console output of the above code snippet.

After remove() - [Mahela, Sanga, Angie, Mahela, Johnson, Mavan, Sanath, Mahela, Sanga]
After removeFirstOccurrence - [Sanga, Angie, Mahela, Johnson, Mavan, Sanath, Mahela, Sanga]
After removeLastOccurrence - [Sanga, Angie, Mahela, Johnson, Mavan, Sanath, Mahela]

Updating elements in the LinkedList

        //Adding an element to the end of the list
        myLinkedList.add("Thirimanne");
        myLinkedList.add("Mahela");
        myLinkedList.add("Sanga");
        myLinkedList.add("Angie");
        myLinkedList.add("Murali");
        myLinkedList.add("Johnson");

        //Updating an element of the linked list
        myLinkedList.set(2, "Guptill");

        System.out.println(myLinkedList);

Here is the console output.

[Thirimanne, Mahela, Guptill, Angie, Murali, Johnson]

Other Important Methods

1. clear()

Removes all the elements from the LinkedList

2. clone()

This method returns a copy of the existing LinkedList

3. offer(E e)

This method can also add a specified element to the LinkedList (to the end of the list in this case). offerFirst(E e) and offerLast(E e) respectively add the element as the first element or as the last element.

According to the Queue interface, the difference between offer() and add() is that add() throws a IllegalStateException if there is no space currently available. It will return true if adding is possible.

On the other hand, offer() will return false when there are issues with the capacity.

But since there is no space issues in LinkedLists, both of these methods can be treated as two methods with the same functionality.

4. peek()

This method returns the first element in the LinkedList. This method also has related methods like peekFirst() and peekLast() to retrieve those relevant elements.

You may have realized that getFirst() and peekFirst() ( or peek() ) are doing the same thing. Well, there is a difference. getFirst() method was introduced when LinkedLists were first introduced. The problem with this method is that it causes NoSuchElementException when there are no elements in the LinkedList.

peek() methods were introduced as a fix for this problem. peek() methods return a null if the LinkedList is empty. It does not cause the program to stop.

4. poll()

This method returns and removes the first element in the LinkedList. This also has its sibling methods pollFirst() and pollLast().

There are two methods that do basically the same thing. remove() methods (removeFirst() to be exact) and pop() method.

Let me simplify things. pop() and returnFirst() does the same thing and have the same issue. Both of them causes NoSuchElementException when the LinkedList is empty. But poll() returns null when the LinkedList is empty. So, I would recommend using the poll() when you want to remove the first element.

5. size()

Returns the size of the LinkedList.


This is it for LinkedLists. Thank you for taking the time to read the article. I hope you guys got a good understanding of LinkedLists now. If you learned something, go ahead and follow the blog to get notifications when I publish a new article. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: