Linked List vs Array With Examples

Linked List and Array are probably the most basic data structures, but their use can often be confusing. The use of the appropriate data structure can often result in an easier and more efficient code. Linked List vs Array is also a popular interview question in data structures.

Linked List vs Array

This article will provide an in-depth comparison of these two data structures.

We will compare them based on the following properties:

  • Definitions and Structures
  • Operations and Time Complexity Analysis
  • Memory Analysis
  • Codes (C and Python)

1. Definitions and Structures

Linked List is a data structure that stores linearly connected, non-contiguous data through references. This means that each node of the linked list would contain a reference to its next and/or previous node. This helps to make a chain of nodes that are linearly connected, but in memory, they may not be in a contiguous segment.

Linked-List (1)

An array is a data structure that has a fixed size and contains a collection of data of a similar type that can be referenced through indexing. This means that before using an array we have to define its size and type and after storing the data we can refer to it using indexing.

In the memory, arrays are present in a contiguous block of data as well.



2D Arrays

2. Operations and Time Complexity Analysis

We will compare the data structures based on the following operations:

  • Insertion and Deletion
  • Accessing Elements

Insertion and Deletion

Insertion and Deletion in Linked List can be done at the beginning, in the middle or at the end.

  • If insertion or deletion is done at the beginning, then we just need to reassign the references at the head thus, this is an O(1) operation.
  • If insertion or deletion is done in the middle or at the end, then we first need to reach the required position in O(N) time and then reassign the references in O(1) time. This takes O(N + 1) = O(N) time.
  • linkedListInsertion


Linked List Insertion

For an array, wherever the insertion or deletion is done, we always need to shift rest of the array to balance the indexing, thus these operations take O(1) time for doing the operation and O(N) time for balancing the indexing. Thus, it takes O(N + 1) = O(N) time always.



Array Insertion

Accessing Elements

In a Linked List, to access an element we have to reach its position through a traversal from the start that takes O(N) time.

In an array, we have indexes which we can directly refer to. This is useful because now, we don’t have to do a traversal and thus, accessing takes O(1) time.

3. Memory Analysis

Linked List is almost always a more memory efficient way to store data. This is because we assign the data in a Linked List dynamically, and its size can be shrunk and expanded according to use.

Arrays on the other hand, always have a fixed size. If an element is not assigned any value, then it still remains a part of the array and will still use up memory.

But this does not mean that arrays are always less efficient. Arrays only take up the memory that they are assigned whereas Linked List will take up memory for storing the data as well as storing the references. Also, for some operations like sorting, we need extra space to store and shift the elements, which is efficient in arrays.

Linked List Implementations

1. Python



2. C



Arrays Implementations

1. Python



2. C



By admin

Leave a Reply

%d bloggers like this: