Doubly Linked List Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Doubly Linked List runs in O(1) insert time and O(n) space, in Swift as in every other language.
- ✓Nothing. The standard library has no linked list of any kind, and Array is a contiguous copy-on-write buffer.
- ✓Mark the node class final, which lets the compiler use static dispatch instead of going through the class vtable on every property access.
- ✓The Swift trap: Automatic Reference Counting is reference counting with no cycle collector at all, so a node holding a strong prev and a strong next keeps its neighbours alive and the entire list leaks, permanently and silently.
- ✓Every node is a heap-allocated class instance with a 16-byte header and an atomic reference count, and the counts are updated on every assignment, which on a multi-threaded traversal means real contention.
What Doubly Linked List actually is
Doubly Linked List in Swift: what you actually reach for
Swift implementation
final class DllNode<T> {
var value: T
// next is strong: the list owns the chain going forward.
var next: DllNode<T>?
// prev MUST NOT be strong. Two strong links in opposite directions
// form an ARC cycle, and ARC has no cycle collector: the list would
// leak in full, forever.
unowned(unsafe) var prev: DllNode<T>?
init(_ value: T) { self.value = value }
}
final class DoublyLinkedList<T> {
private(set) var head: DllNode<T>?
private(set) var tail: DllNode<T>?
private(set) var count = 0
@discardableResult
func append(_ value: T) -> DllNode<T> {
let node = DllNode(value)
node.prev = tail
tail?.next = node
tail = node
if head == nil { head = node }
count += 1
return node
}
func remove(_ node: DllNode<T>) {
let prev = node.prev
let next = node.next
if let prev { prev.next = next } else { head = next }
if let next { next.prev = prev } else { tail = prev }
node.next = nil
count -= 1
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(1) insert
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for Doubly Linked List?
Nothing. The standard library has no linked list of any kind, and Array is a contiguous copy-on-write buffer. Apple ships a Deque in the separate swift-collections package, which covers the double-ended case without nodes.
What is the most common Swift mistake when implementing Doubly Linked List?
Automatic Reference Counting is reference counting with no cycle collector at all, so a node holding a strong prev and a strong next keeps its neighbours alive and the entire list leaks, permanently and silently. Nothing in the compiler warns about it, and the only way to see it is the Leaks instrument. Making prev weak fixes the cycle but adds a side-table indirection on every read; unowned is cheaper because it stores no side-table entry, at the cost of a hard trap if the referent is already gone.
How does Doubly Linked List actually perform in Swift?
Every node is a heap-allocated class instance with a 16-byte header and an atomic reference count, and the counts are updated on every assignment, which on a multi-threaded traversal means real contention. Reading a weak reference is measurably more expensive than reading an unowned one because it must consult the side table to decide whether the object is still alive. Time complexity is O(1) insert and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what GitHub Copilot offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.