Log In Sign Up
Day 9

Day 9: Doubly Linked Lists - Implementation and Comparison

9/60 Days

Doubly Linked Lists - Implementation and Comparison #

Welcome to Day 9 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore doubly linked lists, implement one from scratch, and compare it with singly linked lists.

What is a Doubly Linked List? #

129937

A doubly linked list is a type of linked list where each node contains data and two pointers:

  1. A pointer to the next node
  2. A pointer to the previous node

This bi-directional linking allows for more flexible operations, especially when traversing the list in reverse or deleting nodes.

Implementing a Doubly Linked List #

12nextprev

Let’s implement a doubly linked list with basic operations:

 1class Node:
 2    def __init__(self, data):
 3        self.data = data
 4        self.prev = None
 5        self.next = None
 6
 7class DoublyLinkedList:
 8    def __init__(self):
 9        self.head = None
10        self.tail = None
11
12    def is_empty(self):
13        return self.head is …