Interview Q&A · Java · 5 questions in this topic

LinkedList — Java Interview Q&A

All Java topics

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

  1. 1Indexed get(i) walks from the nearer end, so calling it in a loop turns O(n) work into O(n²).
  2. 2LinkedList is not synchronized; share it only through Collections.synchronizedList or a concurrent queue.
  3. 3Each node pays for two links plus an object header, which matters when millions of elements are stored.

Example

Example.javajava
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

  1. 1Access: RandomAccess array versus sequential node traversal.
  2. 2Search and get: fast on ArrayList, slow on LinkedList.
  3. 3Middle insert and delete: slow shifting on ArrayList, fast link updates on LinkedList.
  4. 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

  1. 1ArrayList: O(1) indexed access, better locality, less memory per element.
  2. 2LinkedList: O(1) add/remove at the head and tail, O(n) get(i).
  3. 3ArrayDeque beats LinkedList for stack and queue workloads.
  4. 4Removal mid-list is O(1) only through a held iterator, not list.remove(i).

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits