Linked List — Using Python

Deepak Belwal
2 min readJul 13, 2023

--

Hi Data Engineers, As we all know that in the our day to day software development we face many problems which are related to different kinds of data structures, so today I will be discussing about one of them.

Lets try to understand this from start, what a linked list is ?
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers.
A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.

Fig: Linked List

Basic code to create a linked list.

Fig: Implementing basic linked list

we created a node class which initialises the node for the linked list, while SlinkedList class helps to initialise the head and a helper function to print the values of the linked list.

Lets discuss a basic interview problem related to linked list.
Ques: Find the middle of a given linked list?

For example, if the given linked list is 1->2->3->4->5->6 then the output should be 4.

5->NULL
The middle element is [5]

4->5->NULL
The middle element is [5]

3->4->5->NULL
The middle element is [4]

2->3->4->5->NULL
The middle element is [4]

1->2->3->4->5->NULL
The middle element is [3]

Time Complexity: O(N), As we are traversing the list once.
Auxiliary Space: O(1), As constant extra space is used.

I hope this was useful for you all, since i faced issues while learning and understanding so i just thought to create it, so that it would help you all.

Keep Reading, Keep Learning
Jai Hind…

--

--

Deepak Belwal

Army lover, Responsible_person, Influencer, Sharing Defence Knowledge, Joining the Armed Forces is my dream, Enthusiast Person, Parakarmo Vijayate, Jai Hind