top of page
Writer's pictureHarini Mallawaarachchi

Introduction to Singly-Linked Lists and Doubly-Linked Lists


Overview

Linked lists are fundamental data structures in computer science that store a collection of elements. They consist of nodes, where each node contains a value and a reference to the next node (and sometimes the previous node in the case of a doubly-linked list). In this article, we will explore the concepts of singly-linked lists and doubly-linked lists, including their characteristics, operations, and Python implementations.


Singly-Linked List


Definition

A singly-linked list is a data structure in which each node contains a value and a reference to the next node. The last node in the list points to None to indicate the end of the list.


Visualization


Singly-Linked List:

  [value] -> [value] -> [value] -> ... -> [None]
      |         |         |
    head      node      node

Implementation in Python


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
        

Operations

  • Insertion: Inserting a node at the beginning or end of the list, or at a specific position.

  • Deletion: Removing a node from the list, given its value or position.

  • Traversal: Iterating through the list to access or modify node values.

  • Searching: Finding a specific node based on its value.

  • Length: Determining the number of nodes in the list.


Doubly-Linked List


Definition

A doubly-linked list is a variation of a linked list where each node contains a value and references to both the next and previous nodes.


Visualization


Doubly-Linked List:

  [None] <-> [value] <-> [value] <-> [value] <-> ... <-> [None]
      |           |           |
    head        node        node

Implementation in Python


class ListNode:
    def __init__(self, val=0, prev=None, next=None):
        self.val = val
        self.prev = prev
        self.next = next
        

Operations

In addition to the operations available in singly-linked lists, doubly-linked lists provide:

  • Reverse Traversal: Iterating through the list in reverse order, from the last node to the first node.

  • Insertion/Deletion: Efficiently inserting or deleting nodes at the beginning or end of the list, or at a specific position.

  • Previous Node Access: Accessing the previous node from a given node.


Advantages of linked lists:

  • Dynamic size

  • Efficient insertion and deletion

  • Flexibility in memory allocation

  • Ease of implementation

  • Efficient for sequential access

  • Lower memory overhead

Disadvantages of linked lists:

  • Slower random access

  • Additional memory for storing pointers

  • Potential cache inefficiency


Conclusion

Singly-linked lists and doubly-linked lists are powerful data structures for managing collections of elements. Singly-linked lists are simple and efficient for most scenarios, while doubly-linked lists offer additional flexibility at the cost of increased memory usage. Understanding the characteristics and operations of these lists enables us to make informed decisions when choosing the appropriate data structure for our specific needs.

By implementing linked lists in Python and utilizing the provided operations, we can harness the power of these data structures to solve various problems efficiently.

3 views0 comments

Recent Posts

See All

BFS & DFS

BFS and DFS are two popular graph traversal algorithms used to visit and explore all the vertices in a graph or tree. Both algorithms...

Greedy Algorithms

Greedy algorithms are a class of algorithms that make locally optimal choices at each step in hopes of achieving a globally optimal...

Divide and Conquer Algorithm

Divide and conquer is a popular algorithmic technique used to solve a wide range of problems. It involves breaking down a problem into...

Comments


bottom of page