DEV Community

HunorVadaszPerhat
HunorVadaszPerhat

Posted on

Java - Singly Linked List and its common methods πŸ“–

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(): Check if the linked list is empty
  • size(): Return the number of elements in the linked list.
  • append(data): Add a new node with the given data to the end of the linked list.
  • prepend(data): Add a new node with the given data to the beginning of the linked list.
  • deleteWithValue(data): Delete the first node with the given value.
  • printList(): Display the linked list.
  • find(data): Search for a node with the given data and return it, or return null if not found.
  • get(index): Return the node at the specified index.
  • clear(): Remove all nodes from the list.
  • insertAt(index, data): Insert a new node with the given data at the specified position.
  • removeAt(index): Remove the node at the specified position.
  • reverse(): 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.

Top comments (0)