Singly linked lists are fundamental data structures in computer science. They consist of nodes containing data and a reference to the next node, forming a linear sequence. This structure allows for efficient insertion and deletion at the beginning of the list.
Singly linked lists have various applications, including implementing undo functionality in text editors and representing polynomials. They offer flexibility in memory allocation but require traversal for accessing elements, making them suitable for specific scenarios where dynamic data management is crucial.
Singly Linked List Fundamentals
Structure of singly linked lists
- Linear data structure consisting of nodes
- Each node contains data and a reference (or link) to the next node (e.g.,
Node { int data; Node* next; }
)
- Last node points to null, indicating the end of the list
- Head pointer points to the first node in the list
- Used to access the entire list
- Tail pointer (optional) points to the last node in the list
- Allows for efficient insertion at the end of the list (e.g.,
append()
operation)
Operations for singly linked lists
- Insertion
- At the beginning
- Create a new node and set its next pointer to the current head
- Update the head pointer to the new node
- Time complexity: $O(1)$
- At the end
- Traverse the list to find the last node
- Create a new node and set the last node's next pointer to it
- Update the tail pointer (if used) to the new node
- Time complexity: $O(n)$
- At a specific position
- Traverse the list to the node before the desired position
- Create a new node and set its next pointer to the next node of the current node
- Update the current node's next pointer to the new node
- Time complexity: $O(n)$
- Deletion
- From the beginning
- Update the head pointer to the next node
- Deallocate the memory of the deleted node
- Time complexity: $O(1)$
- From the end
- Traverse the list to find the second-to-last node
- Set the second-to-last node's next pointer to null
- Update the tail pointer (if used) to the second-to-last node
- Deallocate the memory of the deleted node
- Time complexity: $O(n)$
- From a specific position
- Traverse the list to the node before the desired position
- Update the current node's next pointer to skip the node to be deleted
- Deallocate the memory of the deleted node
- Time complexity: $O(n)$
- Traversal starts from the head and follows the next pointers until reaching null
Singly Linked List Analysis and Applications
Complexity of linked list operations
- Time complexity
- Insertion at the beginning: $O(1)$
- Insertion at the end or a specific position: $O(n)$
- Deletion from the beginning: $O(1)$
- Deletion from the end or a specific position: $O(n)$
- Traversal: $O(n)$
- Space complexity: $O(n)$ for storing $n$ elements
- Linked lists require extra space for storing next pointers compared to arrays
Applications of singly linked lists
- Implementing undo functionality in text editors
- Each action (insertion, deletion, or modification) is stored as a node in a linked list
- Undo operation traverses the list backwards and reverses the actions (e.g., Microsoft Word, Google Docs)
- Representing polynomials
- Each term of the polynomial is stored as a node with coefficient and exponent data (e.g., $3x^2 + 2x + 1$)
- Linked list allows for efficient insertion, deletion, and traversal of terms
- Implementing hash table collision resolution using chaining
- Each bucket in the hash table is a linked list
- Colliding elements are stored in the same bucket's linked list (e.g., separate chaining)
- Representing adjacency lists for graphs
- Each vertex in the graph is represented by a node in a linked list
- Adjacent vertices are stored as nodes in the vertex's linked list (e.g., undirected graphs, directed graphs)