Table of Contents

    Linked List Concept

    Chapter 18.3

    Linked List Concept

    Learn what a linked list is, how it stores data using nodes and links, how it differs from an array, and why linked lists are useful for dynamic memory, insertion, deletion, and flexible data management.

    Introduction

    A linked list is an important linear data structure used to store a collection of elements. Like an array, a linked list can store multiple values. However, the way a linked list stores data is very different from an array.

    In an array, elements are usually stored in continuous memory locations. Each element can be accessed using an index. But in a linked list, elements are stored in separate units called nodes. Each node contains data and a link to the next node.

    A linked list is useful when the number of elements can change frequently. It allows data to be inserted and deleted more flexibly than arrays in many situations. This makes linked lists an important topic in Data Structures and Algorithms.

    "A linked list is a linear data structure where each element is stored in a node, and each node points to the next node."

    Simple Definition of Linked List

    A linked list is a data structure made up of nodes. Each node stores two main things: the actual data and a reference or link to the next node in the list.

    In simple words:

    • A linked list stores data in nodes.
    • Each node contains data and a link.
    • The link connects one node to another node.
    • The first node is called the head.
    • The last node usually points to null or nothing.
    • Linked lists can grow and shrink dynamically.
    LINKED LIST CONCEPT
    Linked List = Node + Link + Sequence
    Important: A linked list does not require elements to be stored continuously in memory. Each node can be located separately, and links connect them together.

    Why Do We Need Linked Lists?

    Arrays are useful, but they have some limitations. In many programming languages, arrays may have fixed size. If we need to add or remove many elements frequently, arrays may require shifting elements or resizing.

    Linked lists solve this problem by storing data in connected nodes. When a new element is added, a new node can be created and linked with existing nodes. This makes insertion and deletion more flexible in many cases.

    Problems with Arrays in Some Cases

    • Array size may be fixed in some languages.
    • Insertion in the middle may require shifting elements.
    • Deletion in the middle may also require shifting elements.
    • Memory may need to be continuous.
    • Resizing may be expensive.
    • Frequent changes can become inefficient.

    Benefits of Linked Lists

    • Size can grow or shrink dynamically.
    • Insertion can be flexible.
    • Deletion can be flexible.
    • Memory does not need to be continuous.
    • Nodes can be created when needed.
    • Useful for dynamic data handling.

    Prerequisites

    Before learning linked lists, students should understand some basic programming and data structure concepts.

    Prerequisite Topic Why It Is Needed
    Variables To understand how data values are stored.
    Objects / Classes Nodes are often represented using classes or objects.
    References / Pointers Links between nodes are created using references or pointers.
    Arrays Helps compare linked lists with array-based storage.
    Loops Used to traverse linked list nodes one by one.
    Conditional Statements Used to check whether a node exists or whether the list is empty.
    Functions / Methods Used to create operations such as insert, delete, search, and display.

    Real-World Analogy of Linked List

    A linked list can be compared to a train. A train is made up of coaches. Each coach is connected to the next coach. If you start from the first coach, you can move from one coach to the next until you reach the last coach.

    Similarly, a linked list is made up of nodes. Each node is connected to the next node. Starting from the first node, the program can move from one node to another using links.

    Linked List as Train Coaches

    Each train coach is like a node. Each coach is connected to the next coach. A linked list works in a similar way by connecting nodes together.

    Train Example Linked List Concept Explanation
    First coach Head node The starting point of the linked list.
    Coach Node Stores data and connection to next node.
    Connector between coaches Link / Reference Connects one node to the next node.
    Last coach Last node The last node points to null.

    What is a Node?

    A node is the basic building block of a linked list. Each node stores data and a link to another node.

    A simple node usually has two parts:

    • Data: The actual value stored in the node.
    • Next: A reference or pointer to the next node.
    NODE STRUCTURE
    Node = Data + Next Link
    
    // Conceptual node structure
    
    Node
    {
        data
        next
    }
    

    For example, if a node stores student marks, the data part may contain 85, and the next part may point to another node containing 92.

    Basic Structure of Linked List

    A linked list starts with a node called the head. The head points to the first node. Each node then points to the next node. The last node points to null, meaning there is no next node.

    
    Head
     |
     v
    [10 | next] -> [20 | next] -> [30 | next] -> null
    

    In this example:

    • Head points to the first node.
    • The first node stores 10 and points to the second node.
    • The second node stores 20 and points to the third node.
    • The third node stores 30 and points to null.

    Important Linked List Terms

    To understand linked lists properly, students should know the following important terms.

    Term Meaning Example / Explanation
    Node Basic unit of linked list. Stores data and link.
    Data Actual value inside a node. 10, 20, Rahul, 85
    Next Reference to the next node. Connects current node to next node.
    Head Starting point of linked list. Points to first node.
    Tail Last node of linked list. Usually points to null in singly linked list.
    null No next node exists. Used at the end of the list.
    Traversal Visiting each node one by one. Start from head and move through next links.

    Types of Linked Lists

    Linked lists can be of different types depending on how nodes are connected. The main types are:

    1

    Singly Linked List

    Each node points only to the next node

    In a singly linked list, each node has data and one link pointing to the next node. Traversal is usually possible in one direction only.

    Example 10 -> 20 -> 30 -> null
    2

    Doubly Linked List

    Each node points to both previous and next nodes

    In a doubly linked list, each node has data, a link to the previous node, and a link to the next node. Traversal is possible in both directions.

    Example null <- 10 <-> 20 <-> 30 -> null
    3

    Circular Linked List

    Last node points back to the first node

    In a circular linked list, the last node does not point to null. Instead, it points back to the first node, forming a circle.

    Example 10 -> 20 -> 30 -> back to 10

    Types of Linked Lists Comparison

    Type Links Per Node Traversal Direction Ending
    Singly Linked List One link: next Forward only Last node points to null
    Doubly Linked List Two links: previous and next Forward and backward Ends usually point to null
    Circular Linked List One or two links depending on type Can loop through nodes circularly Last node points to first node

    Common Operations on Linked Lists

    Like arrays, linked lists support different operations. These operations are important in data structure learning.

    Operation Meaning Example
    Traversal Visit each node one by one. Display all student marks.
    Insertion at Beginning Add new node before current head. Add 5 before 10.
    Insertion at End Add new node after last node. Add 40 after 30.
    Insertion at Position Add new node at a specific location. Add 25 between 20 and 30.
    Deletion Remove a node from the list. Remove node containing 20.
    Searching Find whether a value exists. Search for 30.
    Updating Change data inside a node. Change 76 to 80.

    Traversing a Linked List

    Traversal means visiting each node of a linked list one by one. In a singly linked list, traversal starts from the head and continues using the next link until null is reached.

    
    // Conceptual traversal logic
    
    current = head
    
    while current is not null:
        print current.data
        current = current.next
    

    This logic visits each node and moves from one node to the next node.

    TRAVERSAL RULE
    Start at Head Move using Next Stop at Null

    Insertion in Linked List

    Insertion means adding a new node to the linked list. Linked lists are flexible for insertion because a new node can be created and connected by changing links.

    Insertion at Beginning

    To insert a node at the beginning, the new node points to the current head, and then head becomes the new node.

    
    // Conceptual insertion at beginning
    
    newNode.next = head
    head = newNode
    

    Insertion at End

    To insert a node at the end, the program moves to the last node and connects the last node to the new node.

    
    // Conceptual insertion at end
    
    current = head
    
    while current.next is not null:
        current = current.next
    
    current.next = newNode
    

    Deletion in Linked List

    Deletion means removing a node from the linked list. To delete a node, we adjust links so that the removed node is no longer part of the chain.

    
    // Conceptual deletion idea
    
    previous.next = current.next
    

    If the node to be deleted is the head node, then head should be moved to the next node.

    
    // Deleting first node
    
    head = head.next
    
    Important: In linked lists, deletion usually means changing links, not shifting elements like arrays.

    Searching in Linked List

    Searching means checking whether a specific value exists in the linked list. Since linked lists do not support direct indexing like arrays, searching usually starts from the head and moves node by node.

    
    // Conceptual search logic
    
    current = head
    
    while current is not null:
        if current.data == target:
            print "Found"
            stop
        current = current.next
    
    print "Not found"
    

    Searching in a linked list can be slower than direct array access because nodes must be visited one by one.

    Array vs Linked List

    Arrays and linked lists are both linear data structures, but they store data differently. Understanding the difference helps students choose the correct structure for a problem.

    Basis Array Linked List
    Storage Usually continuous memory. Nodes can be stored separately.
    Access Fast direct access using index. No direct index access; traversal is needed.
    Size May be fixed in some languages. Can grow and shrink dynamically.
    Insertion May require shifting elements. Can be easier by changing links.
    Deletion May require shifting elements. Can be easier by changing links.
    Memory Stores only data values. Stores data plus links, so extra memory is needed.
    Best Use Fast access by position. Frequent insertion and deletion.

    Java Example: Basic Node Concept

    The following Java example shows a simple node structure and how nodes can be linked together.

    Prerequisites: To understand this example, you should know classes, objects, references, constructors, and basic Java syntax.
    
    class Node {
    
        int data;
        Node next;
    
        Node(int value) {
            data = value;
            next = null;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
    
            Node first = new Node(10);
            Node second = new Node(20);
            Node third = new Node(30);
    
            first.next = second;
            second.next = third;
    
            Node current = first;
    
            while (current != null) {
                System.out.println(current.data);
                current = current.next;
            }
        }
    }
    

    In this example, three nodes are created and connected. The program starts from the first node and prints all node values by following the next links.

    JavaScript Example: Basic Node Concept

    JavaScript can also represent linked list nodes using classes.

    Prerequisites: To understand this example, you should know JavaScript classes, objects, constructors, references, and loops.
    
    class Node {
    
        constructor(value) {
            this.data = value;
            this.next = null;
        }
    }
    
    const first = new Node(10);
    const second = new Node(20);
    const third = new Node(30);
    
    first.next = second;
    second.next = third;
    
    let current = first;
    
    while (current !== null) {
        console.log(current.data);
        current = current.next;
    }
    

    This JavaScript example creates three nodes and connects them using the next property.

    Real-World Example: Student Waiting List

    A linked list can be used to manage a student waiting list. Each student can be represented as a node. When a new student is added, a new node can be connected to the list.

    Node Student Name Next Student
    Node 1 Rahul Ayesha
    Node 2 Ayesha John
    Node 3 John Priya
    Node 4 Priya null
    
    Head -> Rahul -> Ayesha -> John -> Priya -> null
    

    This shows how linked nodes can represent a sequence of students.

    Where Linked Lists Are Used

    Linked lists are used in many areas of computer science and software development. They are especially useful when data changes frequently.

    Use Case How Linked List Helps
    Dynamic data storage List can grow or shrink as needed.
    Implementing stacks Nodes can be added and removed from one end.
    Implementing queues Nodes can be added at one end and removed from another.
    Music playlist Each song can point to the next song.
    Browser history Pages can be connected forward and backward using linked structures.
    Memory management Free memory blocks can be managed using linked lists.
    Undo-redo operations Actions can be connected and navigated.

    Advantages of Linked List

    Linked lists provide several advantages compared to arrays in certain situations.

    Benefits of Linked Lists

    • Dynamic size.
    • Can grow and shrink during runtime.
    • Insertion can be efficient if position is known.
    • Deletion can be efficient if node reference is known.
    • No need for continuous memory allocation.
    • Useful for implementing stacks and queues.
    • Good for applications with frequent insertions and deletions.
    • Flexible memory usage compared to fixed-size arrays.

    Limitations of Linked List

    Linked lists are useful, but they also have limitations. They are not always better than arrays.

    No Direct Index Access Unlike arrays, linked lists do not allow direct access using index positions. Traversal is required.
    Extra Memory Needed Each node stores data plus one or more links, so linked lists use extra memory.
    Traversal Can Be Slower To reach a specific node, the program may need to start from the head and move node by node.
    Implementation is More Complex Linked lists are more difficult to implement than arrays because links must be managed carefully.

    When Should You Use Linked List?

    Linked lists are useful when the program needs flexible insertion and deletion, and direct index access is not the main requirement.

    Use Linked List When

    • The number of elements changes frequently.
    • Frequent insertion and deletion are needed.
    • Memory does not need to be continuous.
    • Direct index access is not very important.
    • You are implementing stacks, queues, or dynamic lists.
    • You want to understand pointer or reference-based data structures.

    Common Mistakes Beginners Make

    Linked lists can be confusing at first because students must think about nodes and links. Understanding common mistakes helps avoid errors.

    Common Mistakes

    • Confusing linked list with array.
    • Forgetting to update the head node.
    • Forgetting to connect new nodes properly.
    • Losing reference to the next node during deletion.
    • Creating infinite loops by incorrect linking.
    • Trying to access linked list elements using index directly.
    • Not checking for null before accessing next node.
    • Forgetting that the last node points to null.

    Better Approach

    • Understand node structure clearly.
    • Draw diagrams before writing code.
    • Always track the head node carefully.
    • Use temporary variables during traversal.
    • Check for null before moving to next node.
    • Update links carefully during insertion and deletion.
    • Practice with small linked lists first.
    • Trace each node connection step by step.

    Best Practices for Learning Linked Lists

    Linked lists become easier when students practice them visually. Diagrams are very helpful because links between nodes can be hard to imagine mentally at first.

    Recommended Practices

    • Start with singly linked list first.
    • Understand node structure before operations.
    • Draw nodes and arrows on paper.
    • Practice traversal before insertion and deletion.
    • Use meaningful variable names such as head, current, nextNode.
    • Always check for empty list conditions.
    • Practice insertion at beginning and end first.
    • Practice deletion carefully with diagrams.
    • Compare linked list with array after learning both.
    • Trace code manually to understand link changes.

    Mini Practice Activity

    Complete the following practice tasks to strengthen your understanding of linked lists.

    Task Description Expected Learning
    Task 1 Draw a linked list with three nodes: 10, 20, and 30. Understand node connection.
    Task 2 Identify head, data, next, and null in your diagram. Learn linked list terms.
    Task 3 Write conceptual traversal steps for the linked list. Practice moving node by node.
    Task 4 Insert a new node 5 at the beginning. Understand insertion at head.
    Task 5 Insert a new node 40 at the end. Understand insertion at tail.
    Task 6 Search whether value 20 exists in the linked list. Practice linked list searching.

    Frequently Asked Questions

    1. What is a linked list?

    A linked list is a linear data structure made up of nodes. Each node contains data and a link to the next node.

    2. What is a node?

    A node is the basic unit of a linked list. It usually contains data and a reference to the next node.

    3. What is the head of a linked list?

    The head is the starting point of a linked list. It points to the first node.

    4. What does the last node point to?

    In a singly linked list, the last node usually points to null, meaning there is no next node.

    5. Is linked list a linear data structure?

    Yes. A linked list is a linear data structure because nodes are arranged in a sequence.

    6. What is the difference between array and linked list?

    An array stores elements in continuous memory and supports index access. A linked list stores elements as nodes connected by links and does not support direct index access.

    7. Why are linked lists useful?

    Linked lists are useful because they can grow and shrink dynamically and support flexible insertion and deletion.

    8. What are the main types of linked lists?

    The main types are singly linked list, doubly linked list, and circular linked list.

    9. Can we access a linked list element directly by index?

    Usually no. To access a specific element, we need to start from the head and traverse node by node.

    10. What is traversal in linked list?

    Traversal means visiting each node one by one, starting from the head and following next links until null.

    Summary

    A linked list is a linear data structure made up of nodes. Each node contains data and a link to the next node. The first node is called the head, and the last node usually points to null.

    Unlike arrays, linked lists do not require continuous memory. They are useful when data changes frequently, especially when insertion and deletion operations are common. However, linked lists do not provide direct index-based access like arrays.

    Linked lists are important because they help students understand dynamic data structures, references, pointers, and memory-based connections. They also form the foundation for advanced structures such as stacks, queues, graphs, and memory management systems.

    Key Takeaway

    A linked list stores data using connected nodes. Each node contains data and a link to the next node. Linked lists are useful for dynamic data storage and flexible insertion and deletion operations.