- A node is a structure that contains two main parts:
- Data: The actual value or information that the node stores. This could be an integer, a string, or any other type of data.
- Pointer (or next): A reference to the next node in the list.
- You can imagine a node in a linked list as a car in a train, and the next pointer is like the coupling that connects one train car to the next.
- A Linked List is a linear data structure where elements, called nodes, are connected by pointers. Unlike arrays, linked lists do not store elements in contiguous memory locations.
- The first node is called the head, and it points to the next node in the list.
- Each node contains data and a pointer point to the next node.
- The last node’s pointer is set to
null
. - we using the head pointer to access any node in the linked list.
- We have two cases:
- If this node is the first node in the list:
- If we already have a list, we can insert:
- At the beginning:
- At the end:
- In the middle
- Create a new node and assign a value to it (in my case, I chose
value = 10
). - Create a temporary pointer and point it to the node after which you need to insert the new node.
- Use the next pointer of the current node (that the temporary pointer points to) and point it to the new node.
- Use the next pointer of the new node to point to the next node in the list.
- Create a new node and assign a value to it (in my case, I chose
-
First, check that your list is not empty.
-
Delete from the beginning:
-
Delete from the end
-
Delete from the middle
- Note: We need to delete the node with the
value 3
.
- Note: We need to delete the node with the
Traversing a linked list involves going through each node in the list, starting from the head, and visiting each node one by one.
- Create a pointer that points to the first node.
- Print the value of the node that the pointer points to.
- Move the pointer to the next node and print its value.
- Repeat this process until you reach the end of the list.
-
Singly Linked List
- Structure: Each node has two parts: the data and a pointer (or reference) to the next node in the sequence.
- Traversal: You can traverse the list in only one direction—from the head node to the last node.
- Use Cases: Suitable when you need simple linear traversal with minimal memory usage.
-
Doubly Linked List
- Structure: Each node contains three parts: data, a pointer to the next node, and a pointer to the previous node.
- Traversal: You can traverse the list in both directions (forward and backward).
- Advantages: Efficient backward traversal and easy node deletion.
- Use Cases: Suitable for complex data navigation, like undo/redo functionality in applications.
-
Circular Linked List
- Structure: Similar to a singly linked list, but the last node points back to the head, forming a circular loop.
- Traversal: You can traverse the list indefinitely in a circular fashion. The traversal continues until a specific stopping condition is met.
- Use Cases: Useful in scenarios like round-robin scheduling or in applications that require repeated circular traversal.
- Searching About This:
- Circular Doubly Linked List
- Tail Pointer Linked List
- Skip List
- Dynamic Size: Unlike arrays, linked lists can grow and shrink dynamically during runtime. This provides flexibility in memory management.
- Efficient Insertions/Deletions: Inserting or deleting elements at the beginning or middle of a linked list is generally faster than in an array, as you only need to change pointers rather than shifting elements.
- No Memory Wastage: Memory is allocated as needed, so there’s no need to pre-allocate memory like in arrays, which might waste space if the array is underused.
- Efficient Memory Utilization: Linked lists can easily accommodate variable sizes of data structures, and they don’t require contiguous memory blocks, which can avoid memory fragmentation.
- Higher Memory Overhead: Each element (node) in a linked list requires additional memory for storing the pointer/reference to the next node, which can be inefficient in terms of memory usage.
- Slower Access Time: Linked lists don’t provide direct access to elements, unlike arrays. To access an element, you must traverse the list from the head, which takes O(n) time in the worst case.
- More Complex Operations: Linked lists generally involve more complex code for basic operations (like inserting or deleting a node) compared to arrays, where accessing and modifying elements is simpler.