Time complexity of linked list

Time complexities of different data structures

Time Complexity is a concept in computer science that deals with the quantification of the amount of time taken by a set of code or algorithm to process or run as a function of the amount of input. In other words, the time complexity is how long a program takes to process a given input. The efficiency of an algorithm depends on two parameters:

  • Time Complexity
  • Space Complexity

Time Complexity: It is defined as the number of times a particular instruction set is executed rather than the total time is taken. It is because the total time took also depends on some external factors like the compiler used, processor’s speed, etc.

Space Complexity: It is the total memory space required by the program for its execution.

Average time complexity of different data structures for different operations

Data structure Access Search Insertion Deletion
Array O(1) O(N) O(N) O(N)
Stack O(N) O(N) O(1) O(1)
Queue O(N) O(N) O(1) O(1)
Singly Linked list O(N) O(N) O(1) O(1)
Doubly Linked List O(N) O(N) O(1) O(1)
Hash Table O(1) O(1) O(1) O(1)
Binary Search Tree O(log N) O(log N) O(log N) O(log N)
AVL Tree O(log N) O(log N) O(log N) O(log N)
B Tree O(log N) O(log N) O(log N) O(log N)
Red Black Tree O(log N) O(log N) O(log N) O(log N)

Worst Case time complexity of different data structures for different operations

Data structure Access Search Insertion Deletion
Array O(1) O(N) O(N) O(N)
Stack O(N) O(N) O(1) O(1)
Queue O(N) O(N) O(1) O(1)
Singly Linked list O(N) O(N) O(1) O(1)
Doubly Linked List O(N) O(N) O(1) O(1)
Hash Table O(N) O(N) O(N) O(N)
Binary Search Tree O(N) O(N) O(N) O(N)
AVL Tree O(log N) O(log N) O(log N) O(log N)
Binary Tree O(N) O(N) O(N) O(N)
Red Black Tree O(log N) O(log N) O(log N) O(log N)

Time complexity of linked list

Article Tags :
Analysis
Articles
Data Structures
Difference Between
time complexity
Practice Tags :
Data Structures

Know Thy Complexities!

Hi there! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them. Over the last few years, I've interviewed at several Silicon Valley startups, and also some bigger companies, like Google, Facebook, Yahoo, LinkedIn, and Uber, and each time that I prepared for an interview, I thought to myself "Why hasn't someone created a nice Big-O cheat sheet?". So, to save all of you fine folks a ton of time, I went ahead and created one. Enjoy! - Eric

Check out El Grapho, a graph data visualization library that supports millions of nodes and edges

1. Overview

In this tutorial, we'll talk about the performance of different collections from the Java Collection API. When we talk about collections, we usually think about the List, Map, and Set data structures and their common implementations.

First of all, we'll look at Big-O complexity insights for common operations, and after, we'll show the real numbers of some collection operations running time.

Performance Analysis of ArrayList and LinkedList in Java

A detailed analysis of ArrayList and LinkedList in Java.

by
Anant Mishra
·
Aug. 16, 19 · Performance Zone · Analysis
Like (5)
Comment
Save
Tweet
60.39K Views

Join the DZone community and get the full member experience.

Join For Free

ArrayListandLinkedListare frequently used classes in the Java collection framework. If you know only understand basic performance comparisons ofArrayListandLinkedList, but not the minor details of these two classes, then this article is for you.

" ArrayListshould be used where more search operations are required, and LinkedListshould be used where more insert and delete operation is needed."

ArrayListuses theArraydata structure, and LinkedList uses theDoublyLinkedListdata structure. Here, we are going to discuss how the underlying data structure affects the performance of insert, search, and delete operation onArrayListandLinkedList.

Below is an example of different operations usingArrayListandLinkedList.

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> linkedList = new LinkedList<>(); List<Integer> arrayList = new ArrayList<>(); /*Block 1: Insert at last in LinkedList*/ linkedList.add(1); linkedList.add(111); System.out.println(linkedList); /* Output: [1, 111]*/ /*Block 2: Insert at last in Arraylist*/ arrayList.add(1); arrayList.add(111); System.out.println(arrayList); /* Output: [1, 111]*/ /*Block 3: Insert at given index in LinkedList*/ linkedList.add(1, 11); linkedList.add(3, 1111); System.out.println(linkedList); /* Output: [1, 11, 111, 1111]*/ /*Block 4: Insert at given index in Arraylist*/ arrayList.add(1, 11); arrayList.add(3, 1111); System.out.println(arrayList); /* Output: [1, 11, 111, 1111]*/ /*Block 5: Search by value in LinkedList(searching 111 value)*/ for(int i=0; i < linkedList.size(); i++) { if(linkedList.get(i).equals(111)){ System.out.println("Value found at index: "+i); /* Output: Value found at index: 2*/ } } /*Block 6: Search by value in ArrayList(searching 111 value)*/ for(int i=0; i < arrayList.size(); i++) { if(arrayList.get(i).equals(111)){ System.out.println("Value found at index: "+i); /* Output: Value found at index: 2*/ } } /*Block 7: Get value by index in LinkedList*/ Integer value = linkedList.get(2); System.out.println(value); /* Output: 111*/ /*Block 8: Get value by index in ArrayList*/ value = arrayList.get(2); System.out.println(value); /* Output: 111*/ /*Block 9: Remove by value in LinkedList(remove 111)*/ boolean isDeleted = linkedList.remove(new Integer(111)); System.out.println(isDeleted); /* Output: true*/ /*Block 10: Remove by value in ArrayList(remove 111)*/ isDeleted = arrayList.remove(new Integer(111)); System.out.println(isDeleted); /* Output: true*/ /*Block 11: Remove by index in LinkedList*/ value = linkedList.remove(2); System.out.println("Removed value: "+value); /* Output: Removed value: 1111*/ /*Block 12: Remove by index in ArrayList*/ value = arrayList.get(2); System.out.println("Removed value: "+value); /* Output: Removed value: 1111*/ } }

Now, we will compare similar operations onArrayListandLinkedListand see which is more efficient in terms of performance and why.

Insert Value at Last Index(Block 1 and 2)

When we insert a value at last index, ArrayList has to —

  • Check whether the underlying array is already full or not.

  • If the array is full then it copies the data from the old array to new array(size double than an old array),

  • Then add the value at the last index.

On the other hand, LinkedList simply adds that value at the tail of the underlying DoublyLinkedList.

Both have time complexity O(1), but due to the added steps of creating a new array in ArrayList its worst-case complexity reaches to order of N, that is why we prefer using LinkedList where multiple inserts are required.

Insert Value at Given Index(Block 3 and 4)

When we insert a value at a given index, ArrayList has to-

  • Check whether the underlying array is already full or not.

  • If the array is full then it copies the data from the old array to a new array(size double than an old array).

  • After that, starting from the given index, shift values by one index to create space for the new value.

  • Then add the value at the given index.

On the other hand, LinkedList simply finds the index and adds that value at a given index by rearranging pointers of underlying DoublyLinkedList.

Both have time complexity O(N), but due to the added steps of creating a new array in ArrayList, and copying the existing values to the new index, we prefer using LinkedList where multiple inserts are required.

Search by Value(Block 5 and 6)

When we search any value in ArrayList or LinkedList, we have to iterate through all elements. This operation has O(N) time complexity. It looks like ArrayList and LinkedList both will have the same performance.

Here we need to notice that array(underlying data structure of ArrayList) stores all values in a continuous memory location, but DoublyLinkedList store each node at a random memory location. Iterating through continuous memory location is more performance efficient than random memory location, that is why we prefer ArrayList over LinkedList when searching by value.

Get Element by Index(Block 7 and 8)

When we get an element by Index then ArrayList is a clear winner.

ArrayList can give you any element in O(1) complexity as the array has random access property. You can access any index directly without iterating through the whole array.

LinkedList has a sequential access property. It needs to iterate through each element to reach a given index, so time complexity to get a value by index from LinkedList is O(N).

Remove by Value(Block 9 and 10)

It is similar to adding value at a given index. To remove an element by value in ArrayList and LinkedList we need to iterate through each element to reach that index and then remove that value. This operation is of O(N) complexity.

The difference is that to remove an element from LinkedList, we just need to modify pointers, which is O(1) complexity, but In ArrayList, we need to shift all elements after the index of the removed value to fill the gap created.

As shifting is costly operation then modifying pointers, so even after the same overall complexity O(N), we prefer LinkedList where more delete by value operation is required.

Remove by Index(Block 11 and 12)

To remove by index, ArrayList find that index using random access in O(1) complexity, but after removing the element, shifting the rest of the elements causes overall O(N) time complexity.

On the other hand, LinkedList takes O(N) time to find the index using sequential access, but to remove the element, we just need to modify pointers.

As shifting is costly operation than iterating, so LinkedList is more efficient if we want to delete element by index.

Singly Linked List vs Doubly Linked List

Before looking at the differences between the singly linked list and doubly linked list, we first understand what is singly linked list and doubly linked list separately.