00:00

Array Vs Linked List in Data Structures

Last updated:

Arrays and linked lists are both linear data structures: each keeps its elements in a sequence. They differ in how that sequence is stored in memory, and that one difference decides which operations are fast and which are slow.

This page first recaps each structure, then compares them side by side and in terms of time complexity.

What is an Array in data structure?

An array is a fixed-length block of slots that all hold the same type of value. Each slot is identified by an index starting at 0, and the address of any slot can be calculated from its index, so reading arr[i] takes O(1) time. In Java the length is chosen when the array is created and cannot change later.

A char array of length 10 holding the letters A to J at indexes 0 to 9

In the figure, the array has length 10, so its indexes run from 0 to 9: 'A' is at index 0 and 'J' is at index 9.

What is Linked List in data structure?

A linked list is a chain of separate objects called nodes. Each node holds one value and a reference to the next node. The nodes can be anywhere in memory; the order of the list comes only from these references.

Because a node is created only when a value is added, a linked list has no fixed capacity. It grows and shrinks one node at a time.

Singly linked list: head points to the first node, each node points to the next, and the last node points to null

As the figure shows, the list is reached through a reference to its first node, called the head. The last node, often called the tail, has a next reference of null, which marks the end of the list.

Terminologies for Linked Lists

  • Head: the reference to the first node. Every traversal starts here; if the head is null, the list is empty.

  • Node: one element of the list, made of a data field and a link field.

  • Data: the value the node stores, such as an int or a reference to an object.

  • Next Pointer: the field that refers to the following node, or null in the last node. In Java it is an object reference, not a raw memory address.

Differences between Arrays and Linked Lists

FeatureArrayLinked List
Memory layoutElements sit in one contiguous block.Each node is a separate object that can be anywhere in memory.
SizeFixed when the array is created. Growing means creating a larger array and copying.Changes at run time; a node is added or removed as needed.
Links between elementsNone needed: the position of an element follows from its index.Every node stores a reference to the next node (and, in a doubly linked list, to the previous one).
Access by indexO(1): the address is calculated from the index.O(n): you must follow the links from the head.
Insert/delete at the frontO(n): all other elements must shift.O(1): only the head reference changes.
Insert/delete in the middleO(n) because of shifting.O(1) once you hold a reference to the neighbouring node, but finding that node is O(n).
When memory is allocatedAt run time, in one piece, when the array is created (Java arrays are objects on the heap).At run time, one node at a time.
Memory per elementLowest: only the value. Unused slots still take space.Higher: each value carries one or two extra references and an object header.
Cache performanceGood: neighbouring elements are close in memory.Weaker: following links jumps around memory.
Typical usesLookup tables, matrices, and as the storage behind ArrayList, heaps and hash tables.Queues, stacks, LRU caches, and any list that changes mostly at its ends.

Arrays vs Linked Lists in terms of Time Complexity

OperationArrayLinked List
Access by indexO(1)O(n)
Search for a value (unsorted)O(n)O(n)
Insert/delete at the beginningO(n)O(1)
Insert at the endO(1) if there is free space (amortized O(1) for a dynamic array such as ArrayList)O(1) with a tail reference, otherwise O(n)
Delete at the endO(1)O(n) for a singly linked list (the node before the tail must be found); O(1) for a doubly linked list with a tail reference
Insert/delete at a given positionO(n)O(n) to reach the position, then O(1) to relink