How can you differentiate between Array and LinkedList?

How can you differentiate between Array and LinkedList?

ยท

4 min read

Hey Everyone, This is my first post at hashnode. As I joined hashnode Bootcamp It made me realize to post something here. As I am posting some DSA related stuff on Twitter & LinkedIn using Python for few days it made me realize that let's write an article for everyone. Here you go.....

What is Array?

Array is a data structure that stores the same type of data in memory i.e you can't have multiple data types in an array. Array elements store contiguously in memory location.

Syntax:

array(data_type, value_list)

Example:

Screenshot from 2021-09-25 12-46-55.png

Here "i" is the data type code. Check below the common data types used for the array module.

image.png

What is LinkedList?

LinkedList is the collection of nodes that stores dynamically/randomly in memory. Node has two parts data and link, data is the value store in a node and link holds the address of the next node.

Array ๐Ÿ†š LinkedList

There are so many differences between Array and LinkedList. pointed few of them using examples in below

๐Ÿ‘‰ As we already discussed below point in Array and LinkedList definition:-

๐Ÿ”ธArray stores elements contiguously in the memory location.

๐Ÿ”ธLinkedList nodes stores dynamically/randomly in memory.

See the below pic for a better understanding ๐Ÿ‘‡ MemoryAllocation.jpg

๐Ÿ‘‰ Cost of accessing an element:-

๐Ÿ”ธArray: In the below picture, you can see elements are stored and base addresses are assigned for each element. These base addresses are contiguous for the array i.e 400, 401, 402..... 406,407. So, if you know the base address of an element in an array, then you can get the base address of any element in an array. To get the address of an element you just need to perform a simple operation that's it.

So accessing the elements of an array's cost is O(1).

๐Ÿ”ธLinkedList: As we discussed above LinkedList is a collection of Nodes and that is a collection of two sub-parts i.e., one part contains elements and another one contains the address of the next node. So if you want to access an element in a linked list, you have to know the first element address which is called Head here. If you want to access the 4th node, have to traverse from the first node. Accessing the last node, you have to traverse all the nodes.

So Average time complexity to access an element in LinkedList is O(n).

We can conclude that for accessing an element, Array is the best choice than LinkedList.

Accessing Elements (1).jpg

๐Ÿ‘‰ Cost of Inserting the element at the beginning:-

๐Ÿ”ธArray: If we are adding an element at the beginning, we need to shift other elements to the right to create the space in the first position.

if n numbers of elements are present in the array, the time complexity will be equal to the size of the array i.e., O(n).

๐Ÿ”ธLinkedList: If we need to add a node at the beginning, we don't move any elements anywhere, just create a new node and add the address of the first node to the new node.

This is an easy way to add an element at the beginning. We can say this is just a constant time i.e., O(1).

We can conclude here, for inserting an element at the beginning LinkedList is the best choice than Array.

Inserting_at_beginning.jpg

๐Ÿ‘‰ Cost of Inserting the element at the end:-

๐Ÿ”ธArray: If the array has space, we can insert an element at the end using the array index. In this case, it has constant time complexity i.e O(1).

But If the array is full then we have to copy the array to the new array. So, the time complexity is O(n).

๐Ÿ”ธLinkedList: If we talk about LinkedList, we have to find the head then we can find the last node.

So, we are searching the whole LinkedList to find out the last node to insert an element.

So time complexity is O(n).

Insert Element at End.jpg

๐Ÿ‘‰ Cost of Inserting the element at the middle:-

๐Ÿ”ธArray: If the array has space, insert an element at the middle of the array and moving other elements to the right side of the array.

So the time complexity is O(n).

๐Ÿ”ธLinkedList: If you want to insert an element at LinkedList, you have to traverse from head to that position.

So, the time complexity is equal to the size of LinkedList elements i.e O(n).

Inserting Element at mid.jpg

Based on the above discussion you can choose one or another one of your requirements.

Thank you so much for reading my article. Hope you understood the differences between Array and LinkedList. ๐Ÿ˜Š

ย