Interview Q&A · Java · 5 questions in this topic
LinkedList — Java Interview Q&A
How LinkedList works as a doubly linked list and how it compares with ArrayList. Part of Collections Framework · 294 questions in the Java bank.
What is LinkedList and when is it used?
Answer
LinkedList is a doubly linked implementation in which each element lives in a node holding references to its predecessor and successor. Inserting or removing in the middle is cheap once you have the node, because only nearby references change and no elements shift. It implements both List and Deque, so it exposes deque operations such as getFirst, getLast, addFirst, addLast, removeFirst, and removeLast. It is a good fit for queue, stack, or deque use and for workloads with many middle insertions, but random access by index is slow because traversal starts from an end each time.
Key points
- 1Indexed get(i) walks from the nearer end, so calling it in a loop turns O(n) work into O(n²).
- 2LinkedList is not synchronized; share it only through Collections.synchronizedList or a concurrent queue.
- 3Each node pays for two links plus an object header, which matters when millions of elements are stored.
Example
Deque<String> dq = new LinkedList<>(); dq.addFirst("middle"); dq.addFirst("front"); dq.addLast("back");
ArrayList vs LinkedList?
Answer
ArrayList stores elements in a contiguous array while LinkedList chains doubly linked nodes, and that storage choice drives the trade-off. ArrayList gives fast constant-time get and fast search by index because it implements RandomAccess, while LinkedList must walk node by node from the nearest end. LinkedList wins for frequent insertion and deletion in the middle or at the ends through deque operations. ArrayList uses less memory per element, while each LinkedList node carries two extra references plus object overhead.
Key points
- 1Access: RandomAccess array versus sequential node traversal.
- 2Search and get: fast on ArrayList, slow on LinkedList.
- 3Middle insert and delete: slow shifting on ArrayList, fast link updates on LinkedList.
- 4Memory: compact array versus per-node overhead for next and previous links.
When would you choose LinkedList over ArrayList?
Answer
Almost never for plain list use. ArrayList wins on random access, iteration speed, memory per element and cache locality, and its tail insertion is amortised O(1). LinkedList only pays off when you genuinely need a deque — O(1) add and remove at both ends — or when you remove elements through an iterator you already hold, because that splice is O(1). If the requirement is a queue, ArrayDeque is usually faster than LinkedList, and if it is a list, ArrayList is the default choice.
Key points
- 1ArrayList: O(1) indexed access, better locality, less memory per element.
- 2LinkedList: O(1) add/remove at the head and tail, O(n) get(i).
- 3ArrayDeque beats LinkedList for stack and queue workloads.
- 4Removal mid-list is O(1) only through a held iterator, not list.remove(i).
Why is LinkedList.get(index) O(n)?
Answer
A linked list stores nodes connected by references, with no contiguous array to index into, so reaching position i means walking the chain. LinkedList optimises this by starting from whichever end is nearer: the traversal takes at most size/2 steps. That is still linear and it is why a loop of get(i) over a LinkedList is accidentally quadratic — use an enhanced for loop or an iterator, which walks the nodes once.
Key points
- 1No random access: nodes are reached by following next/prev links.
- 2Traversal starts from the nearer end, halving the constant only.
- 3Indexed loops over a LinkedList are quadratic.
- 4Iterator or for-each traversal is linear.
Example
for (int i = 0; i < list.size(); i++) list.get(i); // O(n²) for (String s : list) { } // O(n)
How do you use a LinkedList safely from multiple threads?
Answer
LinkedList is not thread-safe: concurrent structural changes can corrupt the links or lose elements. Wrap it with Collections.synchronizedList, remembering that iteration still needs a manual synchronized block on the list, or use a concurrent structure such as ConcurrentLinkedDeque or a BlockingQueue implementation. CopyOnWriteArrayList is safe for iteration but is not a LinkedList and copies the whole array on every write, so it suits read-mostly data only.
Key points
- 1Collections.synchronizedList makes individual calls atomic, not iterations.
- 2Iterating a synchronized list requires synchronizing on the list itself.
- 3ConcurrentLinkedDeque is the lock-free double-ended choice.
- 4BlockingQueue is the right tool for producer/consumer queues.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission