Introduction to Linked List

 Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location. The element are linked using pointers.

Why Linked list is used:-

Arrays can be used to store linear data of similar types, but arrays have the following limitations.

  1. Array's size is fixed. So we must know the highest limit of the array in advance. So we must declare the size of the array first and elements there after. 
  2. Inserting a new element is very expensive because the room has to be be created for the new elements and shift the existing elements.

Ex:- Suppose we have arrays in sorted order i.e. [1,2,3,4,6,7] and we have to insert new element i.e. [5] in the array. So in order to maintain elements in sorted order, we have to place that element after suitable element. In this situation we have to shift the element i.e. [6,7] by position 1.

Now we saw the arrays limitations. Let's understand the advantages of the Linked List over array. 

Advantages of Linked List:-

  1. Linked List are in Dynamic in size.
  2. In Linked List, there is Ease of Insertion and deletion of the node.
  3. No memory wastage.
  4. Linked List is traversal and have reverse traversing.

Disadvantages of Linked List:- 

  1. We cannot access elements randomly. We have to access elements sequentially from first element or Node.
  2. Linked List requires extra memory space for pointers.


A Linked List is represented by a pointer to the first node of the linked list. The first node is called "head". If the linked list is empty, then the value of the head is Null.

Each node is consist of atleast two components:-

  1. Data
  2. Pointer points to the next node
Data:- It is  consist of data of the element.
Pointer:- It points to the next node of the Linked List.

Since we learnt about the basics of Linked list and its advantages. Now its time to create Node for basic explanation of Linked List.
First we create Node class, that has it data and pointer variable.
Then we will create LinkedList, that has starting value of Linked Listi.e head.
Let us see the Python's code:-


Let us understand what the above code do. 
Firstly we created a Node class having value of data and next i.e. pointers
Then we created a LinkedList class, that has starting value of Linked List i.e head.

Now, we have to call our LinkedList class using object i.e list1. 
After that we will assign LinkedList's head variable to Node class.
Let us see the Python's code:-


Let us understand the code:- 
  • we create the object i.e. list1 and call the LinkedList class.
  • we assigned the list1.head value to Node class and pass the argument that is data of the node
  • we created 3 Nodes.
  • after that we assigned the next value of the first Node to the second Node and next of second to third Node.
Now we have to create a function to print the element of Linked List one by one.
Let us see the Python's code:-


First we created a temp variable and then start for loop to traverse the Linked List element one by one.

We print the data and then assign next Node value to be printed.

Let us see the full code:-




I hope you find this code useful. If you have any query about this please feel free to comment us.
    Thanks :)

Comments