A Doubly Linked List is a linear data structure where elements are stored in nodes, and each node points to both the next node and the previous node in the sequence. This allows for traversal in both directions, making certain operations more efficient compared to a singly linked list, at the cost of extra memory for the previous pointer.
function insertAtHead(value):
node = new Node(value)
node.next = head.next
node.prev = head
if head.next is not null:
head.next.prev = node
head.next = node
function insertAtTail(value):
node = new Node(value)
cur = head
while cur.next is not null:
cur = cur.next
cur.next = node
node.prev = cur
function insertAt(index, value):
if index == 0:
insertAtHead(value)
return
cur = head
for i = 1 to index:
if cur.next is null: break
cur = cur.next
node = new Node(value)
node.next = cur.next
node.prev = cur
if cur.next is not null:
cur.next.prev = node
cur.next = node
function deleteAt(index):
cur = head
for i = 0 to index:
prev = cur
cur = cur.next
if cur.next is not null:
cur.next.prev = prev
prev.next = cur.next
Sign in to join the discussion
Hand-picked resources to deepen your understanding
© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0