wasAsked.

What is the difference between an array and a linked list?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Data Structures & Algorithms

Community answers

Posted by wasAsked editorial collection

Arrays and linked lists both store sequences of elements, but they organise memory differently, which leads to different performance characteristics. Array - Elements are stored in contiguous memory locations. - Accessing an element by index is O(1): the address is calculated directly (base address + index × element size). - Fixed size in many languages (C# arrays, Java arrays). Dynamic arrays (List<T> in C#, ArrayList in Java, JavaScript arrays, Python lists) grow by allocating a larger array and copying elements, giving amortised O(1) appends. - Inserting or deleting in the middle is O(n), because elements must be shifted. - Excellent cache locality: contiguous memory means CPUs prefetch data efficiently, so iteration is very fast in practice. Linked list - Elements (nodes) are stored anywhere in memory; each node holds its value and a reference to the next node (singly linked) or to both next and previous nodes (doubly linked). - Accessing the k-th element is O(n): you must follow links from the head. - Inserting or deleting at a known node position is O(1) (just change pointers), and inserting at the head is O(1). Finding the position first is still O(n). - Grows and shrinks easily without reallocation or copying. - Extra memory per element for pointers; poor cache locality because nodes are scattered, which makes traversal slower in practice than big-O suggests. Complexity comparison - Access by index: array O(1); linked list O(n). - Search by value: both O(n) (O(log n) for a sorted array with binary search). - Insert or delete at the beginning: array O(n); linked list O(1). - Insert or delete at the end: dynamic array amortised O(1) for insert and O(1) for delete; linked list O(1) with a tail pointer (doubly linked for O(1) delete at the tail). - Insert or delete in the middle (given the node): array O(n); linked list O(1). - Memory overhead: array low; linked list higher (pointers per node). Simple singly linked list in C# public class Node<T>(T value) { public T Value { get; } = value; public Node<T>? Next { get; set; } } public class SinglyLinkedList<T> { private Node<T>? _head; public void AddFirst(T value) => _head = new Node<T>(value) { Next = _head }; // O(1) public IEnumerable<T> Items() { for (var n = _head; n != null; n = n.Next) yield return n.Value; } } Built-in types - C#: T[] and List<T> (dynamic array); LinkedList<T> (doubly linked). - Java: arrays and ArrayList; LinkedList. - In most real applications, dynamic arrays (List<T>, ArrayList) are the default because random access and cache-friendly iteration matter more than O(1) middle insertions. When to use a linked list - Frequent insertions and deletions at the ends or at known positions, such as implementing queues, deques, LRU caches (combined with a hash map), undo histories, or adjacency lists in graphs. - When reallocating large arrays would be costly or memory is fragmented. Common interview follow-ups - Reverse a linked list (iteratively by re-pointing next references). - Detect a cycle (Floyd's tortoise and hare: slow and fast pointers). - Find the middle element (slow and fast pointers). - Merge two sorted lists. Interview summary: "Arrays store elements contiguously, giving O(1) indexed access and great cache performance but O(n) inserts and deletes in the middle. Linked lists store nodes connected by pointers, giving O(1) insertions and deletions at known positions but O(n) access and more memory overhead."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics