Java - Singly Linked List and its common methods 📖
Tags: java, computerscience, singlylinkedlist, dsa
Published: 2023-08-27 12:50:28

First off, what's a Singly Linked List? 🤔

Of course! Let's delve into the fundamentals of a Singly Linked List.

### **Singly Linked List (SLL)** 

A Singly Linked List (often abbreviated as SLL) is a linear data structure that consists of a sequence of elements, where each element points to the next element in the sequence.

### **Core Components:** 📦

1. **Node**: Each element in the list is called a node. Every node has two parts:
   - **Data**: The actual value/data the node holds.
   - **Next**: A reference (or link) to the next node in the sequence.

2. **Head**: The first node in the list. If you know the head, you can traverse the entire list.

3. **Tail (optional in some implementations)**: The last node in the list, which points to nothing (usually denoted as null or None).



### **Visualization**: 🚂

Think of a train made up of a series of cars connected end-to-end. The engine (head) leads the way, and each car (node) has a connection only to the car behind it. The last car (tail) doesn't have any car connected behind it, symbolizing the end of the list.

### Common Methods:
This is a list of the common methods PLUS a link to an article that gives a short straight to the point description:
- [**isEmpty()**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-and-the-isempty-method-2a07): Check if the linked list is empty
- [**size()**](https://dev.to/hunorvadaszperhat/java-understanding-the-singly-linked-list-counting-nodes-with-size-16dm): Return the number of elements in the linked list.
- [**append(data)**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-how-to-append-like-a-pro-o1o): Add a new node with the given data to the end of the linked list.
- [**prepend(data)**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-starting-at-the-top-with-prependdata-mif): Add a new node with the given data to the beginning of the linked list.
- [**deleteWithValue(data)**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-goodbyes-with-deletewithvaluedata-2gh6): Delete the first node with the given value.
- [**printList()**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-showcasing-with-printlist-2ecc): Display the linked list.
- [**find(data)**](https://dev.to/hunorvadaszperhat/java-singly-linked-lists-playing-detective-with-finddata-2pe2): Search for a node with the given data and return it, or return null if not found.
- [**get(index)**](https://dev.to/hunorvadaszperhat/java-finding-your-nodes-place-with-getindex-41f6): Return the node at the specified index.
- [**clear()**](https://dev.to/hunorvadaszperhat/java-spring-cleaning-with-the-clear-7o9): Remove all nodes from the list.
- [**insertAt(index, data)**](https://dev.to/hunorvadaszperhat/java-insertatindex-data-in-your-singly-linked-list-2nn1): Insert a new node with the given data at the specified position.
- [**removeAt(index)**](https://dev.to/hunorvadaszperhat/java-removeatindex-cleaning-with-one-index-at-a-time-mk): Remove the node at the specified position.
- [**reverse()**](https://dev.to/hunorvadaszperhat/java-reverse-turning-your-singly-linked-list-world-upside-down-5g9l): Reverse the linked list.

### **Properties & Features**: 🧠

1. **Dynamic Size**: Unlike arrays, singly linked lists don't have a fixed size. They can grow or shrink dynamically as needed.

2. **Traversal**: To access or find an element in a SLL, you have to start from the head and traverse through the list until you find the desired element.

3. **Memory**: Since each node holds its data and a reference to the next node, SLLs can sometimes be more memory-consuming than arrays, especially when the overhead of storing references is considered.

4. **Insertions & Deletions**: Adding or removing nodes from a SLL (especially at the beginning) can be faster than arrays because there's no need to shift other elements.

### **Why Use a Singly Linked List?** 🔗

- When you need a data structure with a dynamic size.
- When you have frequent insertions and deletions and don't require direct access to elements.
- When you want to implement other data structures like stacks or queues.

### **Limitations**: 🎢 

- No direct access to individual elements. You need to traverse the list from the head to reach a specific element.
- Consumes more memory per element than arrays due to storage of next references.
- Since there's only a link to the next node and not the previous, you can't traverse backward through the list (unless you implement a doubly linked list).

In a nutshell, a Singly Linked List is a versatile data structure with its unique advantages and limitations. Understanding when to use it versus other data structures is crucial for efficient problem-solving in programming.