Singly Linked List: Explained, Examples, and Applications
Table of Contents
- Introduction of singly linked list
- Node Structure
- Basic Operations
- Insertion
- Inserting a node at the beginning of the list.
- Inserting a node at the end of the list.
- Inserting a node at a specific position in the list.
- Deletion
- Deleting the first node in the list.
- Deleting the last node in the list.
- Deleting a node at a specific position in the list
- Traversal
- Searching
- Reversing the List
- Real-world applications where Singly Linked Lists are used
- Advantages and disadvantages of using Singly Linked Lists in specific scenarios
- Conclusion
- FAQs
- What is a Singly Linked List?
- How do you insert a node at the beginning of a Singly Linked List?
- What is the time complexity for searching an element in a Singly Linked List?
- How do you delete a node from a Singly Linked List?
- What are the advantages and disadvantages of using Singly Linked Lists?
Introduction of singly linked list
A Singly Linked List is a fundamental data structure in computer science, representing a collection of nodes where each node stores a data element and a reference to the next node in the sequence. Unlike arrays, linked lists do not have a fixed size, allowing for dynamic memory allocation as elements are added or removed.
The structure of a Singly Linked List consists of nodes connected in a linear sequence, with each node containing data and a pointer/reference to the next node. This organization enables efficient insertion and deletion operations, as well as traversal through the list.
Singly Linked Lists are versatile and find applications in various scenarios, including implementing stacks, queues, and dynamic memory allocation. Understanding the concepts and operations of Singly Linked Lists is essential for any programmer, as they form the basis for more complex data structures and algorithms. In this tutorial, we will explore the fundamentals of Singly Linked Lists, covering their structure, basic operations, and practical applications.
Node Structure
The node structure of a Singly Linked List consists of two main components: data and a reference to the next node in the sequence. Here’s an example of a typical node structure for a Singly Linked List in Java.
public class SinglyLinkedList {
private Node head; // Head of the linked list
// Node structure
private static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Method to insert a node at the beginning of the list
public void insertAtBeginning(int data) {
Node newNode = new Node(data); // Create a new node with the given data
newNode.next = head; // Set the next reference of the new node to the current head
head = newNode; // Update the head to point to the new node
}
// Method to display the linked list
public void display() {
Node current = head; // Start from the head of the list
while (current != null) {
System.out.print(current.data + " "); // Print the data of the current node
current = current.next; // Move to the next node
}
System.out.println(); // Print a newline after displaying all nodes
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.insertAtBeginning(3);
list.insertAtBeginning(5);
list.insertAtBeginning(7);
list.display(); // Output: 7 5 3
}
}In this node structure:
data
represents the value stored in the node.next
is a reference to the next node in the sequence. It is initialized tonull
if the node is the last node in the list.- The constructor initializes a node with the provided data and sets the next reference to
null
. - Getters and setters are provided to access and modify the data and next reference of the node.
This node structure forms the building blocks of a Singly Linked List, allowing for the creation of a linear sequence of nodes where each node contains data and a reference to the next node in the list.