Circular 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
- ✓Circular Linked List runs in O(1) insert time and O(n) space, in Swift as in every other language.
- ✓Nothing for rings, and nothing for linked lists either. Array with modular indexing is the standard library answer, and the swift-collections package adds a Deque for the double-ended case.
- ✓Index into an Array with (i + 1) % count and keep the storage flat. A class-node ring should be a last resort in Swift, because the memory model punishes it more than any other language here.
- ✓The Swift trap: Two distinct traps. A class-node ring is a permanent ARC leak with no escape: making one link weak works for a doubly linked list because the backward pointer is not part of the forward cycle, but in a ring every edge is, so weakening any of them breaks the structure.
- ✓Array is contiguous with copy-on-write, so a ring over an array is cache-resident and passing it around is free until someone mutates a shared copy.
What Circular Linked List actually is
Circular Linked List in Swift: what you actually reach for
Swift implementation
// The Swift answer: flat storage, modular index, no reference cycles.
struct RingBuffer<T> {
private var storage: [T]
private var head = 0
init(_ items: [T]) {
precondition(!items.isEmpty, "ring must be non-empty")
self.storage = items
}
var count: Int { storage.count }
mutating func next() -> T {
defer { head = (head + 1) % storage.count }
return storage[head]
}
mutating func previous() -> T {
// (head - 1) % count is WRONG in Swift: % on a negative Int
// returns a negative value, and the subscript then traps.
head = (head - 1 + storage.count) % storage.count
return storage[head]
}
func lap(from start: Int = 0) -> [T] {
(0..<storage.count).map { storage[(start + $0) % storage.count] }
}
}
var rr = RingBuffer(["w1", "w2", "w3"])
let assigned = (0..<5).map { _ in rr.next() }
// ["w1", "w2", "w3", "w1", "w2"]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 Circular Linked List?
Nothing for rings, and nothing for linked lists either. Array with modular indexing is the standard library answer, and the swift-collections package adds a Deque for the double-ended case.
What is the most common Swift mistake when implementing Circular Linked List?
Two distinct traps. A class-node ring is a permanent ARC leak with no escape: making one link weak works for a doubly linked list because the backward pointer is not part of the forward cycle, but in a ring every edge is, so weakening any of them breaks the structure. Second, and far more common: the remainder operator on a negative Int returns a negative result in Swift, so stepping backwards with (i - 1) % n yields -1 and traps on the next subscript. The correct form is (i - 1 + n) % n.
How does Circular Linked List actually perform in Swift?
Array is contiguous with copy-on-write, so a ring over an array is cache-resident and passing it around is free until someone mutates a shared copy. Integer remainder compiles to a division; when the capacity is a power of two, & (n - 1) is measurably faster and is what the standard library does internally. 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 Replit 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.