Linked List Quick Start
Description
Linked lists store values in nodes connected by references. Singly and doubly linked lists share the same API for typical operations; doubly linked lists additionally maintain prev references for efficient reverse operations.
Creation
Import classes
from dsa.singlylinkedlist import LinkedList, Node as SNode
from dsa.doublylinkedlist import DoublyLinkedList, Node as DNode
Create an empty singly linked list
ll = LinkedList()
Create an empty doubly linked list
dll = DoublyLinkedList()
Nodes-Only Construction
Build a singly linked list using nodes
# Create nodes and link them
n1 = SNode(1)
n2 = SNode(2)
n3 = SNode(3)
n1.next = n2
n2.next = n3
# Initialize the list with head/tail
ll = LinkedList(head=n1, tail=n3, count=3)
Build a doubly linked list using nodes
# Create nodes and link them with next/prev
d1 = DNode(1)
d2 = DNode(2)
d3 = DNode(3)
d1.next = d2
d2.prev = d1
d2.next = d3
d3.prev = d2
# Initialize the list with head/tail
dll = DoublyLinkedList(head=d1, tail=d3, count=3)