Linked Lists — what’s that?

Alwaz Qazi
3 min readAug 30, 2021

--

Hey Dear Readers…!!!

Welcome to another blog.

The topic I’m going to cover today is Linked List. So if you don’t know what is it then stay tune because by the end of this blog you’ll be able to traverse through a Linked List.

Let’s get started…

Linked List is basically a Data structure that is used to store collection of items.

lets say we have these numbers

6,3,4,2,1

Now, you must be wondering we can store these numbers in an array then why Linked List? well Linked list works differently then an array does.

The difference between an Array and Linked List is.

An Array can be visualized as a long box with many partitions of the same size, as shown in figure below.

Array visualization

Where as Linked list can be visualized as many boxes connected (linked) with each other. As shown in fig below.

Visualization of Linked List

Now, lets take a look at the implementation of Linked List.

Now each of these boxes in a Linked List can be represented as objects of class Box.

Now this class is going to have two attributes in it.

Now, lets say in the image below if we write

head.data ### result 4
head.next ### will point to the address of next node
head.next.data ### will point to the data in next node i.e 6

So, when we implement linked list we rename the Box class to Node.

Now we’ll see how we can create that list shown in the picture above. As I mentioned above, the boxes are the objects of Box(Node) class. so let’s create those objects.

The first Node in the linked list is called head. Initially it is None which means we don’t have any data(box) in our list.

Now, there are some conditions to follow while traversing through Linked List.

  1. check if the link list is empty.
  2. If it is not empty then traverse through each node.

Implementing fist condition.

Implementing second condition.

Now, lets print this method will definitely print “list is empty” because we haven’t stored any data yet.

In this blog we learnt about how you can travers through linked list I’ll continue this series will I will explain about how you can

add

remove

items from LinkedList.

That’s all for today folks see you in next blog!!!

--

--

Alwaz Qazi
Alwaz Qazi

Responses (1)