An Introduction to Linked Lists in Python

A linked list is a data structure used for storing a sequence of elements. It’s data with some structure (the sequence).

Implementation of Linked List

class LinkedList():
def __init__(self):
self.head = None

def append(self, value):
if self.head is None:
self.head = Node(value)
current_node = self.head
while is not None:
current_node = = Node(value)

def show_elements(self):
current = self.head
while current is not None:
current =

def length(self):
result = 0
current = self.head
while current is not None:
result += 1
current =
return result

def get_element(self, position):
i = 0
current = self.head
while current is not None:
if i == position:
current =
i += 1
return None

How to reverse a Linked List?

def reverse(l):
if l.head is None:

current_node = l.head
prev_node = None

while current_node is not None:
# Track the next node
next_node =

# Modify the current node = prev_node

# Update prev and current
prev_node = current_node
current_node = next_node

l.head = prev_node

Real World Applications of Linked Lists

  1. Image viewer — Previous and next images are linked, hence can be accessed by next and previous button.
  2. Previous and next page in web browser — We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.
  3. Music Player — Songs in music player are linked to previous and next song. you can play songs either from starting or ending of the list.




Follow me on my #100DaysOfCode journey on Twitter →

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cloud Resume Challenge Part 1: Static S3 Website, CloudFront & DNS

Go with the Flow Monthly News / December, 2021

Automation of Web Apps Using Docker, Jenkins and GitHub.

What Is Bug Triage And What Advantages Does It Have?

Attracting Powerups with a Magnet

What Are the Top Differences between JavaScript vs Python?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditi Deodhar

Aditi Deodhar

Follow me on my #100DaysOfCode journey on Twitter →

More from Medium

Sorting Algorithms: Insertion Sort

Time Complexity of Python Data Structures

48. Rotate Image

C++ Datastructures and Algorithms Final Exam Solutions