Internal working of linkedlist in java - geeksforgeeks

LinkedList in Java

Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. It also has a few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach a node we wish to access.

How Does LinkedList work Internally?

Since a LinkedList acts as a dynamic array and we do not have to specify the size while creating it, the size of the list automatically increases when we dynamically add and remove items. And also, the elements are not stored in a continuous fashion. Therefore, there is no need to increase the size. Internally, the LinkedList is implemented using the doubly linked list data structure. The main difference between a normal linked list and a doubly LinkedList is that a doubly linked list contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list.

Linked List Data Structure

  • Last Updated : 01 Feb, 2022

Practice Problems on Linked List
Recent Articles on Linked List

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:

Internal working of linkedlist in java - geeksforgeeks

In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.

Topics :

  • Singly Linked List
  • Circular Linked List
  • Doubly Linked List

  • Misc
  • Quick Links

Singly Linked List :

  1. Introduction to Linked List
  2. Linked List vs Array
  3. Linked List Insertion
  4. Linked List Deletion (Deleting a given key)
  5. Linked List Deletion (Deleting a key at given position)
  6. Write a function to delete a Linked List
  7. Find Length of a Linked List (Iterative and Recursive)
  8. Search an element in a Linked List (Iterative and Recursive)
  9. Write a function to get Nth node in a Linked List
  10. Nth node from the end of a Linked List
  11. Print the middle of a given linked list
  12. Write a function that counts the number of times a given int occurs in a Linked List
  13. Detect loop in a linked list
  14. Find length of loop in linked list
  15. Function to check if a singly linked list is palindrome
  16. Remove duplicates from a sorted linked list
  17. Remove duplicates from an unsorted linked list
  18. Swap nodes in a linked list without swapping data
  19. Pairwise swap elements of a given linked list
  20. Move last element to front of a given Linked List
  21. Intersection of two Sorted Linked Lists
  22. Intersection point of two Linked Lists.
  23. QuickSort on Singly Linked List
  24. Segregate even and odd nodes in a Linked List
  25. Reverse a linked list

More >>

Circular Linked List :

  1. Circular Linked List Introduction and Applications,
  2. Circular Linked List Traversal
  3. Split a Circular Linked List into two halves
  4. Sorted insert for circular linked list
  5. Check if a linked list is Circular Linked List
  6. Convert a Binary Tree to a Circular Doubly Link List
  7. Circular Singly Linked List | Insertion
  8. Deletion from a Circular Linked List
  9. Circular Queue | Set 2 (Circular Linked List Implementation)
  10. Count nodes in Circular linked list
  11. Josephus Circle using circular linked list
  12. Convert singly linked list into circular linked list
  13. Circular Linked List | Set 1 (Introduction and Applications)
  14. Circular Linked List | Set 2 (Traversal)
  15. Implementation of Deque using circular array
  16. Exchange first and last nodes in Circular Linked List

More >>

Doubly Linked List :

  1. Doubly Linked List Introduction and Insertion
  2. Delete a node in a Doubly Linked List
  3. Reverse a Doubly Linked List
  4. The Great Tree-List Recursion Problem.
  5. Copy a linked list with next and arbit pointer
  6. QuickSort on Doubly Linked List
  7. Swap Kth node from beginning with Kth node from end in a Linked List
  8. Merge Sort for Doubly Linked List
  9. Create a Doubly Linked List from a Ternary Tree
  10. Find pairs with given sum in doubly linked list
  11. Insert value in sorted way in a sorted doubly linked list
  12. Delete a Doubly Linked List node at a given position
  13. Count triplets in a sorted doubly linked list whose sum is equal to a given value x
  14. Remove duplicates from a sorted doubly linked list
  15. Delete all occurrences of a given key in a doubly linked list
  16. Remove duplicates from an unsorted doubly linked list
  17. Sort the biotonic doubly linked list
  18. Sort a k sorted doubly linked list
  19. Convert a given Binary Tree to Doubly Linked List | Set
  20. Program to find size of Doubly Linked List
  21. Sorted insert in a doubly linked list with head and tail pointers
  22. Large number arithmetic using doubly linked list
  23. Rotate Doubly linked list by N nodes
  24. Priority Queue using doubly linked list
  25. Reverse a doubly linked list in groups of given size
  26. Doubly Circular Linked List | Set 1 (Introduction and Insertion)
  27. Doubly Circular Linked List | Set 2 (Deletion)

More >>

Misc :

  1. Skip List | Set 1 (Introduction)
  2. Skip List | Set 2 (Insertion)
  3. Skip List | Set 3 (Searching and Deletion)
  4. Reverse a stack without using extra space in O(n)
  5. An interesting method to print reverse of a linked list
  6. Linked List representation of Disjoint Set Data Structures
  7. Sublist Search (Search a linked list in another list)
  8. How to insert elements in C++ STL List ?
  9. Unrolled Linked List | Set 1 (Introduction)
  10. A Programmer’s approach of looking at Array vs. Linked List
  11. How to write C functions that modify head pointer of a Linked List?
  12. Given a linked list which is sorted, how will you insert in sorted way
  13. Can we reverse a linked list in less than O(n)?
  14. Practice questions for Linked List and Recursion
  15. Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
  16. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
  17. Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
  18. Squareroot(n)-th node in a Linked List
  19. Find the fractional (or n/k – th) node in linked list
  20. Find modular node in a linked list
  21. Construct a linked list from 2D matrix
  22. Find smallest and largest elements in singly linked list
  23. Arrange consonants and vowels nodes in a linked list
  24. Partitioning a linked list around a given value and If we don’t care about making the elements of the list “stable”
  25. Modify contents of Linked List

Quick Links :

  • ‘Practice Problems’ on Linked List
  • ‘Videos’ on Linked List
  • ‘Quizzes’ on Linked List

If you still need more assistance with your placement preparation, have a look at our Complete Interview Preparation Course. The course has been designed by our expert mentors to help students crack the coding interview of top product or service-based organizations . You get access to premium lectures, 200+ coding questions bank, resume building tips, and lifetime access to the course content. So to make sure that your next programming interview doesn’t feel like an interrogation, enroll in Complete Interview Preparation and give a boost to your placement preparation.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Internal working of linkedlist in java - geeksforgeeks


Java LinkedList class

Internal working of linkedlist in java - geeksforgeeks

Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

The important points about Java LinkedList are:

  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to occur.
  • Java LinkedList class can be used as a list, stack or queue.

Hierarchy of LinkedList class

As shown in the above diagram, Java LinkedList class extends AbstractSequentialList class and implements List and Deque interfaces.

Doubly Linked List

In the case of a doubly linked list, we can add or remove elements from both sides.

Internal working of linkedlist in java - geeksforgeeks

LinkedList class declaration

Let's see the declaration for java.util.LinkedList class.

Constructors of Java LinkedList

ConstructorDescription
LinkedList()It is used to construct an empty list.
LinkedList(Collection<? extends E> c)It is used to construct a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.

Methods of Java LinkedList

MethodDescription
boolean add(E e)It is used to append the specified element to the end of a list.
void add(int index, E element)It is used to insert the specified element at the specified position index in a list.
boolean addAll(Collection<? extends E> c)It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean addAll(Collection<? extends E> c)It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean addAll(int index, Collection<? extends E> c)It is used to append all the elements in the specified collection, starting at the specified position of the list.
void addFirst(E e)It is used to insert the given element at the beginning of a list.
void addLast(E e)It is used to append the given element to the end of a list.
void clear()It is used to remove all the elements from a list.
Object clone()It is used to return a shallow copy of an ArrayList.
boolean contains(Object o)It is used to return true if a list contains a specified element.
Iterator<E> descendingIterator()It is used to return an iterator over the elements in a deque in reverse sequential order.
E element()It is used to retrieve the first element of a list.
E get(int index)It is used to return the element at the specified position in a list.
E getFirst()It is used to return the first element in a list.
E getLast()It is used to return the last element in a list.
int indexOf(Object o)It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
int lastIndexOf(Object o)It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
ListIterator<E> listIterator(int index)It is used to return a list-iterator of the elements in proper sequence, starting at the specified position in the list.
boolean offer(E e)It adds the specified element as the last element of a list.
boolean offerFirst(E e)It inserts the specified element at the front of a list.
boolean offerLast(E e)It inserts the specified element at the end of a list.
E peek()It retrieves the first element of a list
E peekFirst()It retrieves the first element of a list or returns null if a list is empty.
E peekLast()It retrieves the last element of a list or returns null if a list is empty.
E poll()It retrieves and removes the first element of a list.
E pollFirst()It retrieves and removes the first element of a list, or returns null if a list is empty.
E pollLast()It retrieves and removes the last element of a list, or returns null if a list is empty.
E pop()It pops an element from the stack represented by a list.
void push(E e)It pushes an element onto the stack represented by a list.
E remove()It is used to retrieve and removes the first element of a list.
E remove(int index)It is used to remove the element at the specified position in a list.
boolean remove(Object o)It is used to remove the first occurrence of the specified element in a list.
E removeFirst()It removes and returns the first element from a list.
boolean removeFirstOccurrence(Object o)It is used to remove the first occurrence of the specified element in a list (when traversing the list from head to tail).
E removeLast()It removes and returns the last element from a list.
boolean removeLastOccurrence(Object o)It removes the last occurrence of the specified element in a list (when traversing the list from head to tail).
E set(int index, E element)It replaces the element at the specified position in a list with the specified element.
Object[] toArray()It is used to return an array containing all the elements in a list in proper sequence (from first to the last element).
<T> T[] toArray(T[] a)It returns an array containing all the elements in the proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
int size()It is used to return the number of elements in a list.

Java LinkedList Example

Output: Ravi Vijay Ravi Ajay


Java LinkedList example to add elements

Here, we see different ways to add elements.

Initial list of elements: [] After invoking add(E e) method: [Ravi, Vijay, Ajay] After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay] After invoking addAll(Collection<? extends E> c) method: [Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addAll(int index, Collection<? extends E> c) method: [Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addFirst(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addLast(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat, Harsh]


Java LinkedList example to remove elements

Here, we see different ways to remove an element.

Initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking remove(index) method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] Updated list : [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit, Ravi, Hanumat] After invoking removeAll() method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking removeFirst() method: [Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking removeLast() method: [Gaurav, Harsh, Virat, Gaurav, Harsh] After invoking removeFirstOccurrence() method: [Harsh, Virat, Gaurav, Harsh] After invoking removeLastOccurrence() method: [Harsh, Virat, Gaurav] After invoking clear() method: []


Java LinkedList Example to reverse a list of elements

Output: Ajay Vijay Ravi


Java LinkedList Example: Book

Output:

101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6

Next TopicArrayList vs LinkedList



← prev next →



Data Structures in Background- Java Collection Framework

Internal working of linkedlist in java - geeksforgeeks

As a developer we use various data structures to deal with different kind of data to meet certain requirements. We need to decide which data structure is best suited for our requirements.

Talking particularly for Java, we are provided with Collection of classes & interfaces which is popularly known as Collection framework. This has several useful classes & these…

1. Overview

In this article, we are going to explore the most popular implementation of Map interface from the Java Collections Framework in more detail, picking up where our intro article left off.

Before we get started with the implementation, it's important to point out that the primary List and Set collection interfaces extend Collection but Map does not.

Simply put, the HashMap stores values by key and provides APIs for adding, retrieving and manipulating stored data in various ways. The implementation is based on the the principles of a hashtable, which sounds a little complex at first but is actually very easy to understand.

Key-value pairs are stored in what is known as buckets which together make up what is called a table, which is actually an internal array.

Once we know the key under which an object is stored or is to be stored, storage and retrieval operations occur in constant time, O(1) in a well-dimensioned hash map.

To understand how hash maps work under the hood, one needs to understand the storage and retrieval mechanism employed by the HashMap. We'll focus a lot on these.

Finally, HashMap related questions are quite common in interviews, so this is a solid way to either prepare an interview or prepare for it.