|
| 1 | +# Linked List in Python |
| 2 | + |
| 3 | +A **Linked List** is a fundamental **linear data structure** where elements are **not** stored at contiguous memory locations (unlike arrays or Python lists). Instead, the elements, called **Nodes**, are linked together using **pointers** or **references**. |
| 4 | + |
| 5 | +This structure allows for highly efficient **insertions** and **deletions** compared to arrays, where these operations can be slow. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Structure of a Linked List |
| 10 | + |
| 11 | +The linked list is built upon two core concepts: |
| 12 | + |
| 13 | +1. **Node:** The basic building block, which contains: |
| 14 | + * **Data:** The value stored. |
| 15 | + * **Next Pointer (`next`):** The reference to the next node in the sequence. |
| 16 | +2. **Head:** A pointer to the very first node in the list. It is the entry point for all operations. The last node's `next` pointer always points to **`None`**. |
| 17 | + |
| 18 | +### Real-Life Analogy |
| 19 | + |
| 20 | +Think of a **treasure hunt or a chain of clues**. Each clue (**Node**) holds the information (**Data**) and a direction to the next clue (**Next Pointer**). You must follow the chain from the beginning (**Head**) to find the end. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Python Implementation: The Node Class |
| 25 | + |
| 26 | +Since Python doesn't have a built-in linked list type, we define its structure using classes. |
| 27 | + |
| 28 | +The `Node` class defines the element structure. |
| 29 | + |
| 30 | +```python |
| 31 | +class Node: |
| 32 | + """Represents a single element in the linked list.""" |
| 33 | + def __init__(self, data): |
| 34 | + # Store the data |
| 35 | + self.data = data |
| 36 | + # Initialize the pointer to the next node |
| 37 | + self.next = None |
| 38 | +``` |
| 39 | + |
| 40 | +## Python Implementation: The LinkedList Class |
| 41 | + |
| 42 | +The `LinkedList` class manages the list, primarily by keeping track of the `head`. |
| 43 | + |
| 44 | +```python |
| 45 | +class LinkedList: |
| 46 | + """Manages the linked list and its head pointer.""" |
| 47 | + def __init__(self): |
| 48 | + # Initialize the list's entry point |
| 49 | + self.head = None |
| 50 | +``` |
| 51 | + |
| 52 | +### Traversal: Printing the List |
| 53 | + |
| 54 | +To traverse, we start at the head and loop until the current node becomes None, updating the current node with its next pointer in each iteration. |
| 55 | + |
| 56 | +```python |
| 57 | +class LinkedList: |
| 58 | + # __init__ and Node class definition here |
| 59 | + |
| 60 | + def print_list(self): |
| 61 | + current_node = self.head |
| 62 | + print("List:", end=" ") |
| 63 | + while current_node is not None: |
| 64 | + print(current_node.data, end=" -> ") |
| 65 | + current_node = current_node.next |
| 66 | + print("None") |
| 67 | +``` |
| 68 | +***Example*** |
| 69 | + |
| 70 | +```python |
| 71 | +my_list = LinkedList() |
| 72 | +my_list.head = Node(10) |
| 73 | +second = Node(20) |
| 74 | +third = Node(30) |
| 75 | + |
| 76 | +# Linking the nodes: 10 -> 20 -> 30 -> None |
| 77 | +my_list.head.next = second |
| 78 | +second.next = third |
| 79 | +my_list.print_list() |
| 80 | +``` |
| 81 | + |
| 82 | +**Output:** |
| 83 | + |
| 84 | +``` |
| 85 | +List: 10 -> 20 -> 30 -> None |
| 86 | +``` |
| 87 | + |
| 88 | +### Insertion Operations |
| 89 | + |
| 90 | +#### 1. Insertion at the Beginning (Prepend) |
| 91 | + |
| 92 | +It only requires updating the head pointer. |
| 93 | + |
| 94 | +```python |
| 95 | +def insert_at_beginning(self, new_data): |
| 96 | + # Create a new node |
| 97 | + new_node = Node(new_data) |
| 98 | + |
| 99 | + # Make the new node's next pointer point to the current head |
| 100 | + new_node.next = self.head |
| 101 | + |
| 102 | + # Update the list's head to point to the new node |
| 103 | + self.head = new_node |
| 104 | +``` |
| 105 | + |
| 106 | +***Example*** |
| 107 | + |
| 108 | +```python |
| 109 | +my_list.insert_at_beginning(5) |
| 110 | +my_list.print_list() |
| 111 | +``` |
| 112 | + |
| 113 | +**Output:** |
| 114 | + |
| 115 | +``` |
| 116 | +List: 5-> 10 -> 20 -> 30 -> None |
| 117 | +``` |
| 118 | + |
| 119 | +#### 2. Insertion After a Node |
| 120 | + |
| 121 | +```python |
| 122 | +def insert_after(self, prev_node, new_data): |
| 123 | + if prev_node is None: |
| 124 | + print("Previous node cannot be None.") |
| 125 | + return |
| 126 | + |
| 127 | + # Create the new node |
| 128 | + new_node = Node(new_data) |
| 129 | + |
| 130 | + # Set new node's next to the previous node's next |
| 131 | + new_node.next = prev_node.next |
| 132 | + |
| 133 | + # Set the previous node's next to the new node |
| 134 | + prev_node.next = new_node |
| 135 | +``` |
| 136 | + |
| 137 | +***Example*** |
| 138 | + |
| 139 | +```python |
| 140 | +my_list.insert_after(my_list.head, 15) |
| 141 | +my_list.print_list() |
| 142 | +``` |
| 143 | + |
| 144 | +**Output:** |
| 145 | + |
| 146 | +``` |
| 147 | +List: 5-> 15-> 10 -> 20 -> 30 -> None |
| 148 | +``` |
| 149 | + |
| 150 | +### Deletion Operation |
| 151 | + |
| 152 | +Deleting the Head |
| 153 | + |
| 154 | +```python |
| 155 | +def delete_head(self): |
| 156 | + if self.head is None: |
| 157 | + return |
| 158 | + |
| 159 | + # Move the head to the next node, which effectively "deletes" the old head |
| 160 | + self.head = self.head.next |
| 161 | +``` |
| 162 | + |
| 163 | +***Example*** |
| 164 | + |
| 165 | +```python |
| 166 | +my_list.delete_head() # Deletes head (5) |
| 167 | +my_list.print_list() |
| 168 | +``` |
| 169 | + |
| 170 | +**Output:** |
| 171 | + |
| 172 | +``` |
| 173 | +List: 15-> 10 -> 20 -> 30 -> None |
| 174 | +``` |
0 commit comments