Linked Lists Group 9
Linked Lists Group 9
1. Introduction
A linked list is a linear data structure where elements are stored in nodes, and each
node points to the next one in the sequence. Unlike arrays, linked lists do not store
elements in contiguous memory locations.
Structure of a Node
each node contains:
Data Field: Holds the actual data.
Next Field: A pointer/reference to the next node.
Nodes are stored wherever there is free space in memory, the nodes do not have to be
stored contiguously right after each other like elements are stored in arrays. Another
thing with linked lists is that when adding or removing nodes, the rest of the nodes in
the list do not have to be shifted. the first node in a linked list is called the "Head", and
the last node is called the "Tail".
Code structure of a node
class Node:
def __init__=(self, data):
self.data = data # Store the data
self.next = None # Pointer to the next node (initially None)
Comparison with arrays
Linked lists consist of nodes, and is a linear data structure we make ourselves, unlike
arrays which is an existing data structure in the programming language that we can
use. Nodes in a linked list store links to other nodes, but array elements do not need to
store links to other elements.
Arrays vs linked lists in memory
The linked list has four nodes with values 3, 5, 13 and 2, and each node has a pointer
to the next node in the list. Each node takes up four bytes. Two bytes are used to store
an integer value, and two bytes are used to store the address to the next node in the
list.
When removing or inserting elements in an array, every element that comes after must
be either shifted up to make place for the new element, or shifted down to take the
removed element's place. Such shifting operations are time consuming and can cause
problems in real-time systems for example. The image below shows how elements are
shifted when an array element is removed.
Differences between arrays and linked lists
Advantages:
✔ Less Memory Usage – Only one pointer per node (to the next node), reducing memory
overhead.
✔ Efficient Insertion/Deletion at Head – Can insert or delete at the beginning in O(1)
time.
✔ Dynamic Size – Can grow/shrink in size without needing predefined memory like an
array.
Disadvantages:
✘ Slow Access Time (O(n)) – Cannot access elements directly by index (like arrays).
Traversal is required.
✘ One-way Traversal – Cannot move backward, only forward.
✘ Deletion at Tail is Costly (O(n)) – To delete the last node, full traversal is needed.
2. Doubly Linked List (DLL)
o Each node has three parts:
Data
Pointer to next node
Pointer to previous node
o Allows traversal in both directions.
Implementation
Advantages:
✔ Two-way Traversal – Can move both forward and backward through the list.
✔ Efficient Deletion (O(1)) – Can delete a node without traversal if a pointer to it
is given.
✔ More Flexible Operations – Easier to implement complex structures like stacks
and queues.
Disadvantages:
✘ More Memory Usage – Each node stores two pointers, doubling memory
requirements.
✘ Insertion Complexity – Inserting requires updating two pointers instead of one.
✘ More Overhead – More processing needed for maintaining extra pointers.
Advantages:
Disadvantages:
Implementation
Advantages:
Disadvantages:
✘ More Memory Usage – Each node has two pointers (next and prev), increasing
space complexity.
✘ Complex Implementation – Pointer management is harder due to circular
structure.
✘ Risk of Infinite Loops – If traversal conditions are incorrect, it can cause an
infinite loop.
A. Insertion
1. At the Beginning
o Allocate memory for a new node.
o Point the new node to the current head.
o Update head to the new node.
2. At the End
o Traverse to the last node.
o Set its next pointer to the new node.
o Make the new node's next pointer NULL.
3. At a Given Position
o Traverse to the previous node of the target position.
o Update pointers accordingly.
B. Deletion
C. Searching
• Traverse the list and compare each node’s data with the target value.
D. Traversal
• Start from the head and visit each node until reaching NULL.