Linked List vs Array When to Choose Which in DSA C++ - Complexity Comparison
Choosing between a linked list and an array affects how fast your program runs for different tasks.
We want to know how their time costs grow as we add more items.
Analyze the time complexity of inserting an element at the start for both array and linked list.
// Insert at start in array
void insertAtStartArray(int arr[], int &size, int value) {
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = value;
size++;
}
// Insert at start in linked list
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtStartList(Node* &head, int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
This code shows how adding an item at the start works differently in arrays and linked lists.
Look at what repeats when inserting at the start.
- Primary operation in array: Shifting all existing elements one step right.
- How many times: Once for each existing element (n times).
- Primary operation in linked list: Creating a new node and changing two pointers.
- How many times: Always once, no matter how many elements.
As the list or array grows, see how the work changes.
| Input Size (n) | Array Insert Steps | Linked List Insert Steps |
|---|---|---|
| 10 | 10 shifts | 1 pointer change |
| 100 | 100 shifts | 1 pointer change |
| 1000 | 1000 shifts | 1 pointer change |
Pattern observation: Array insert grows linearly with size; linked list insert stays constant.
Time Complexity: O(n) for array insert at start, O(1) for linked list insert at start
This means adding at the start gets slower with bigger arrays but stays fast with linked lists.
[X] Wrong: "Arrays and linked lists have the same speed for all operations."
[OK] Correct: Arrays need to move many items to insert at start, but linked lists just change pointers, so their speeds differ a lot depending on the task.
Understanding these differences helps you pick the right tool for the job and explain your choices clearly in interviews.
"What if we changed the insertion to the end of the array and linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand random access requirement
Random access means accessing any element directly by its position quickly.Step 2: Compare array and linked list access times
Arrays allow direct access by index in constant time, while linked lists require traversal.Final Answer:
Array -> Option BQuick Check:
Random access = Array [OK]
- Thinking linked lists provide fast random access
- Confusing stack or queue with random access
- Ignoring traversal cost in linked lists
Solution
Step 1: Identify dynamic array syntax in C++
Dynamic arrays use pointers with 'new' keyword and size in brackets.Step 2: Check each option
int* arr = new int[10]; correctly uses 'int* arr = new int[10];'. Others are either static arrays or incorrect syntax.Final Answer:
int* arr = new int[10]; -> Option DQuick Check:
Dynamic array = new int[size] [OK]
- Using static array syntax for dynamic arrays
- Missing pointer (*) in declaration
- Incorrect use of parentheses instead of brackets
struct Node {
int data;
Node* next;
};
Node* head = new Node{1, nullptr};
head->next = new Node{2, nullptr};
head->next->next = new Node{3, nullptr};
Node* temp = head;
while(temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}Solution
Step 1: Understand linked list creation
Nodes are created with values 1, 2, 3 linked in order: head -> 1 -> 2 -> 3 -> nullptr.Step 2: Trace the while loop output
Loop prints data of each node from head to end: 1, then 2, then 3, each followed by space.Final Answer:
1 2 3 -> Option AQuick Check:
Traverse linked list prints 1 2 3 [OK]
- Confusing order of nodes
- Forgetting to update temp pointer
- Assuming array-like indexing
struct Node {
int data;
Node* next;
};
void insertAtHead(Node* head, int val) {
Node* newNode = new Node{val, nullptr};
newNode->next = head;
head = newNode;
}Solution
Step 1: Analyze function parameter passing
head is passed by value, so changes inside function do not affect original head pointer.Step 2: Understand effect on linked list
Assigning head = newNode only changes local copy; original list head remains unchanged.Final Answer:
head pointer is passed by value, so original list is not updated -> Option AQuick Check:
Pass pointer by reference to update list head [OK]
- Assuming pointer changes affect caller without reference
- Incorrect next pointer assignment
- Ignoring memory management
Solution
Step 1: Understand operation requirements
Frequent insertions and deletions at random positions require efficient element rearrangement.Step 2: Compare array and linked list for these operations
Arrays require shifting elements on insert/delete, which is costly. Linked lists adjust pointers, making these operations efficient.Final Answer:
Linked List, because it allows efficient insertions and deletions without shifting elements -> Option CQuick Check:
Frequent insert/delete = Linked List [OK]
- Choosing array for frequent insert/delete ignoring shift cost
- Thinking linked list supports fast random access
- Confusing binary search applicability
Ready to prove it?
Pass the test and this pattern moves unknown
