CS 201 › Lesson 1 of 12

Arrays, Linked Lists & Complexity

Lesson 1 · OKSTEM College · AS Computer Science

Arrays & Linked Lists

The two most fundamental data structures. Every higher-level structure (stacks, queues, trees, graphs) is implemented on top of them.

Arrays

OperationTimeReason
Access by indexO(1)Direct address = base + i × size
Search (unsorted)O(n)Must scan all elements
Insert at endO(1) amortizedDynamic array doubles when full
Insert in middleO(n)Must shift elements right
Delete in middleO(n)Must shift elements left

Singly Linked Lists

class Node: def __init__(self, val): self.val = val self.next = None # pointer to next node # Linked list: O(1) insert at head, # O(n) access by index, O(n) search

Trade-off: Arrays give O(1) random access but expensive middle insertions. Linked lists give O(1) head insert/delete but no random access. Choose based on workload.

Big-O Review

For DS&A, you must internalize complexity for all operations:

StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Linked ListO(n)O(n)O(1)*O(1)*
Hash TableO(1) avgO(1) avgO(1) avg
BST (balanced)O(log n)O(log n)O(log n)

*at known position

Lab — Array vs Linked List Visualizer

Knowledge Check

Array access by index is O(1) because

Inserting in the middle of an array is O(n) because

A linked list has O(n) access because

When would you prefer a linked list over an array?

Dynamic arrays (like Python lists) achieve O(1) amortized append by

Next →