Data Structures · Visualization
Doubly LL: Traverse
Doubly Linked List
Traversing a doubly linked list works like a singly linked list, but it can go in either direction: forward from head using 'next', or backward from tail using 'prev'. Time: O(n). Space: O(1).
Big-O: Insert/delete at either end O(1); extra memory for prev pointers.
Doubly LL: Traverse
Doubly Linked ListTraversing a doubly linked list works like a singly linked list, but it can go in either direction: forward from head using 'next', or backward from tail using 'prev'. This flexibility is the main advantage over a singly linked list.
How Traverse Works
- To go forward: start at head, follow 'next' until reaching null.
- To go backward: start at tail, follow 'prev' until reaching null.
- Process (read/print) each node's value as you visit it.
Traverse Worked Example
Input: null <- 5 <-> 10 <-> 15 -> null
- Forward from head: visit 5, 10, 15
- Backward from tail: visit 15, 10, 5
Output: Forward: 5, 10, 15 | Backward: 15, 10, 5
Press ▶ Run to watch it step by step.
def traverse(head):
cur = head
while cur:
print(cur.val, end=" -> ")
cur = cur.next # follow next pointer
print("None")
# example: traverse on [5, 10, 15]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Traverse Time & Space Complexity
When to Use Traverse
- Displaying a playlist forward or in reverse
- Scrolling browser history both backward and forward
- Any feature needing bidirectional iteration
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.