Linked list vs Array
Published: 2024-04-15 01:27:53

---
title: Linked list vs Array
published: true
date: 
tags: 
canonical_url: https://www.pynerds.com/data-structures/linked-list-vs-array/
---

[Linked lists](https://www.pynerds.com/data-structures/singly-linked-list-in-python/) and arrays are two commonly used data structures. Each has its own advantages and disadvantages, and is suitable for  specific scenarios than others. In this article we will compare the two data structures on various basis.

## Definitions

An array is a data structure that stores a fixed-size collection of elements of the same type. The elements are stored contiguously in memory and each element can be accessed using an index. Lists in Python are implemented through arrays.

```

arr = ['Java', 'Python', 'Ruby', 'C++'] # an array

#get an element at specific index
print(arr[2]) # the element at the third position

```

On the other hand, a linked list is a linear data structure in which elements are connected through references. Each element, also known as a **node** , contains a data field and a reference to the next node in the list.  Basically, a node in a linked list will look as shown below:

```

class Node:
    def __init__ (self, element, nxt):
        self.data = element #Users element
        self.next = nxt #The next node in the list
```

The  `data` instance attribute keeps the value of the element stored by the node,  while the `next `attribute stores a reference to the next node in the list.

## Insertion and Deletion

### Arrays

Insertion and deletion in an array can be expensive, especially if the size is large and the element is inserted or deleted from the beginning or middle of the array.

When you insert or delete an element at an arbitrary position in an array, each elements after that position shifts.

In case with insertion, the elements shifts  to the right, creating a gap for the new element to be inserted. The following figure illustrates this better.

![shift elements in an array to create space.](https://res.cloudinary.com/dgc50iktu/image/upload/v1708311493/c48feb68-f7a9-4be0-b4d1-8aaaa5c6bbe1_pskxry.png)

![After element in an array shifts](https://res.cloudinary.com/dgc50iktu/image/upload/v1708311469/5e41c387-65a5-4752-8726-c0c9c6d4470e_u95pdd.png)

![After inserting the element.](https://res.cloudinary.com/dgc50iktu/image/upload/v1708311462/b596c2e8-9402-4075-a828-b8c7960becd7_lj5cib.png)

With deletion, the elements shifts to the left to occupy the space that has been left by the removed element.

![the element to be removed from the array](https://res.cloudinary.com/dgc50iktu/image/upload/v1708312496/90d9a7b5-e916-468b-b6f5-4c8452fb0e8c_vzb5ek.png)

![A hole created](https://res.cloudinary.com/dgc50iktu/image/upload/v1708312503/ca7fc7ba-03dc-4077-a183-2820f5aa936b_s0qocn.png)

![Elements shifts](https://res.cloudinary.com/dgc50iktu/image/upload/v1708312511/4e41c593-ddf1-4da3-b494-4ab4214e7bb1_gohfhi.png)

![array after elements have shifted](https://res.cloudinary.com/dgc50iktu/image/upload/v1708312521/ca8ff32a-d02f-4547-907d-c63d978ca075_sj91hb.png)

Insertion and deletion in arrays have a worst case time complexity of `O(n)`, as all the elements after the insertion or deletion shifts.This can be costly for large arrays with a lot of elements.

The only exception is when insertion or deletion happens at the end of the array, as there is no need to shift any elements. Insertion and deletion at the last position has a time complexity of `O(1)`. This makes the end of an array the best place to insert or delete elements if possible

#### Linked list

Insertion and deletion in a linked list is easier and much more efficient compared to in array. In a linked list, both insertion and deletion have a constant time complexity, `O(1)`.

With insertion, the new element can be inserted at any position by just updating the relevant nodes to reference the new node. While with deletion, an element can simply be dereferenced and removed from the list without affecting the references of other nodes. This is because each node only has a reference to the next node, so removing one node does not affect the references of other nodes.

## Accessing elements

Accessing elements in an array is much more efficient compared to in a linked list. The elements in arrays are stored in contagious memory locations, we can, therefore, directly access an element using its index. This ensures a constant time complexity(`O(1)`) regardless of the size of the array or the location of the element to be accessed.

On the other hand, elements in linked list are scattered throughout the memory, thus we have to traverse the entire list to access a specific element.  Since the elements are not stored contiguously we cannot use index to access an element, we have to traverse the list starting from the head node until we reach the desired element Thus, accessing elements has a worst case time complexity of `O(n)`.

## Memory allocation and efficiency

Arrays are more memory-efficient as they only require memory for the individual elements, and not for any additional references. However, they also have a fixed size that cannot be changed, this makes them less flexible and not very suitable for representing  dynamic data.

A linked list requires extra memory to store the references, which can be disadvantageous if the list contains a large number of elements. On the better side, memory allocation for a linked list is dynamic, meaning new nodes can be allocated as and when needed. This makes linked lists flexible for adding or removing elements.

## Summary

| Array | Linked list |
| --- | --- |
| A collection of homogeneous items in which an item can be accessed using its index | A sequence of nodes where each node references the next node in the list.  |
| Have a fixed size that is defined at declaration. | Are of dynamic size. |
| Faster access to elements as each element can be accessed with its index at a time complexity of `O(1)`. | Accessing elements is inefficient and has a worst time complexity of `O(n)`. |
| Insertion and deletion operations become more inefficient the closer you move to the beginning of the array. Both has a worst time complexity of `O(n)` | Very efficient insertion and deletion operations. Both operations have a time complexity of `O(1)` at any position. |
| Relatively less memory footprint as only the elements are stored. | More memory footprint, as references to other nodes have to be stored in addition to the actual element. |
<div class="related"><h3>Related articles</h3><hr><ul><li><p><a class="text-success" href="https://www.pynerds.com/list-data-type-in-python/">List Data Type in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/complexity-functions-in-algorithm-analysis/">Complexity Functions in algorithm analysis</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/implement-stack-data-structure-in-python/">Implement Stack data structure in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/implement-queue-data-structure-in-python/">Implement Queue data structure in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/implement-fixed-length-stack-in-python/">Implement Fixed-length stack in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/double-ended-queue-in-python/">Double-ended Queue in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/doubly-linked-list-in-python/">Doubly-Linked List in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/binary-trees-in-python/">Binary Trees in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/tree-traversal-algorithms/">Tree traversal algorithms</a><li><p><a class="text-success" href="https://www.pynerds.com/data-structures/implement-selection-sort-in-python/">Implement selection sort in Python</a></ul></div>