DEV Community

Cover image for Lets talk about linked list
Raiyan Rahman
Raiyan Rahman

Posted on

Lets talk about linked list

It's gonna be small blog on what is linked list, how they work and what they do.

So what are linked list?

Linked list is basically a type of data structuer where the main objects of the structure are its nodes and pointers of those nodes.
Nodes are basically objects that consist of a value(it's not limited my any data structure) and a pointer than points to the next node.

Class Node:
    def __init__(self, value):
        self.value = value
        self.next = //Next value
Enter fullscreen mode Exit fullscreen mode

There is a starting of a linked list called "Head" and an end of it called "Tail". Head points to the first value and tail points to the last value. Elements of the list can be accessed by traversing through a loop. Just like list it has features like deleting, instering, changing values etc with its own time and space complexity

Visual representation of linked list

Difference between linked list and list:

The main difference between linked list and list is how they are arrange in the memory. List are arranged in a linear manner with indexing.

visual representation List in memory
And as for linked list they're scattered over the memory pointing with pointers to next element

visual representation Linked list in memory

The key use of Linked list:

The main use case of linked list is for insertion and deletion. Generally in list to delete or insert an item it's needed to left shift or right shift the other elements of the list as it's in a continious memory location

Left shifing a list
But it's different for linked list as the locations of the elements of linked list are scattered and connected through a pointers only the few pointers needed to be changed. It has a big impact on time complexity if the dataset is large enough.

Changing pointers of a linked list

Overview and Core Differences between linked list and list:

  • Memory location: In list elements are stored in a contiguous space in memory where as in linked list it's scattered throughout the memory
  • Access time: List can be accessed throuhg indexes, as for linked list it's needed to be traversed to access elements of it.
  • Insersion and deletion: The items in a list needed to be left shifted or right shifted for deletion or insertion of an element and for linked list only pointers needed to be changed

Top comments (0)