Python Complete Lecture: Linked Lists in
Python
A) Title:
Understanding Linked Lists in Python Programming
B) Learning Objectives:
By the end of this lecture, students will be able to:
Understand the concept of Linked Lists.
Differentiate Linked Lists from Arrays.
Implement a singly linked list in Python.
Perform basic operations: Insertion, Deletion, Traversing.
Apply real-world scenarios of Linked Lists.
C) Real-Life Motivation:
“Imagine railway bogies connected in a chain — each bogie knows where the
next one is but not all at once. Similarly, Linked Lists allow data to be stored
in scattered memory, with each node pointing to the next.”
✔ Modern Tech Examples:
Dynamic Memory Allocation
Undo/Redo Functionality in Software
Browser History Tracking
D) Theoretical Explanation:
1. What is a Linked List?
A Linked List is a linear data structure where elements are stored in
nodes, and each node points to the next node.
Each Node Contains:
Data: The value of the node.
Next: A reference to the next node.
2. Array vs. Linked List:
Array Linked List
Fixed size Dynamic size
Random access Sequential access
Memory allocated Scattered memory
contiguously
Costly insert/delete Easy insert/delete
3. Structure of a Node in Python:
class Node:
def __init__(self, data):
[Link] = data
[Link] = None
4. Creating a Linked List:
class LinkedList:
def __init__(self):
[Link] = None
5. Inserting Elements:
Insert at the Beginning:
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = [Link]
[Link] = new_node
Insert at the End:
def insert_at_end(self, data):
new_node = Node(data)
if [Link] is None:
[Link] = new_node
return
temp = [Link]
while [Link]:
temp = [Link]
[Link] = new_node
6. Traversing (Printing) the Linked List:
def display(self):
temp = [Link]
while temp:
print([Link], end=' -> ')
temp = [Link]
print("None")
7. Deleting a Node:
Delete First Node:
def delete_first(self):
if [Link]:
[Link] = [Link]
Delete Last Node:
def delete_last(self):
if [Link] is None:
return
if [Link] is None:
[Link] = None
return
temp = [Link]
while [Link]:
temp = [Link]
[Link] = None
8. Example Usage:
ll = LinkedList()
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.insert_at_beginning(5)
[Link]()
ll.delete_first()
[Link]()
E) Common Student Questions:
1. Why use Linked Lists instead of arrays?
2. Is there random access in Linked Lists?
3. What happens if you delete a node incorrectly?
F) Practice Tasks:
✔️Task 1: Create a linked list and insert 3 elements.
✔️Task 2: Display the linked list.
✔️Task 3: Delete the first and last elements.
✔️Task 4: Insert an element at the beginning.
G) Summary:
Linked Lists store elements as nodes connected via pointers.
Ideal for dynamic memory allocation.
Insertion and deletion are easier than arrays.
Not suitable for quick (random) access.
H) Quiz (3 Questions):
1. Can you access a random element directly in a linked list? (Yes/No)
2. What pointer does the last node point to?
3. Which is more memory-efficient for fixed-size data — Array or Linked
List?
I) Homework:
✔️Implement a linked list that performs:
Insert at beginning.
Insert at end.
Delete a specific node (by value).
✔️Write a program to reverse a linked list.
J) Viva/Interview Preparation:
✔️What is the difference between singly and doubly linked lists?
✔️When is a linked list preferred over an array?
✔️Explain how insertion in a linked list works?
✔️Can you have a circular linked list?
K) Real World Example (Bonus):
✔️Music Playlist: Each song points to the next.
✔️Web Browser History: Back and Forward buttons use linked lists.
✔️Undo/Redo in Word Processors: Previous and next states are linked.
Final Note:
“Linked Lists teach you how to think in chains — not everything has to be
next to each other, yet everything remains connected!”
Prepared By:
Anesh Meghwar
IMCS University of Sindh