Quick revision
The short answer
A linked list is a chain of nodes, each holding a value and a reference, next, to the following node; the list is reached through its first node, the head, and the last node's next is null. Because nodes can sit anywhere in memory, adding or unhooking a node at a known place is O(1) link work, while reaching position i means following i links, O(n). Doubly linked lists add a prev reference, and java.util.LinkedList is one.
The solution
What it costs
| Operation or property | Array or ArrayList | Linked list |
|---|---|---|
| Read element i | O(1), direct address | O(n), follow i links |
| Insert or remove at the front | O(n), shift everything | O(1) |
| Insert or remove at the end | O(1) amortised | O(1) with a tail reference |
| Insert or remove given a node | O(n), shift | O(1) if doubly linked |
| Memory per value | the value only | value plus one or two references |
| Walking in order | fast, values side by side | slower, nodes scattered |
What to remember
A linked list is a train of nodes, each holding a value and one coupling to the next, reached from the head and ending in null. It gives up reading by position in O(1) and gains O(1) changes to its shape wherever we already have a finger on the right node, and keeping head, tail and size correct through every change is most of the craft. The clue to remember is a question that hands you nodes and asks you to change their order or find a place without knowing the length: think in arrows, and before changing any next, keep a reference on every node you would otherwise lose.
1A train of coaches, and what a node is
Think of a goods train. Each coach carries some cargo and has one coupling that holds the coach behind it. The coaches do not need to be painted with numbers, and they do not need to know where they are on the track; each one only needs to hold on to the next. To reach the fifth coach, you start at the engine and walk back coach by coach. A linked list is this train, built in a computer's memory.
Each coach is called a node. A node is a small object that holds two things. The first is the value, the cargo, such as the number 20. The second is a reference to the next node, usually called next. In Java a reference is a variable that names an object somewhere in memory, like a finger pointing at it, and people often call it a pointer or a link. The list as a whole is reached through its first node, called the head. The last node has nothing after it, so its next is null, Java's word for refers to nothing.
If this sounds abstract, let us build one on paper. Draw three boxes and write 10, 20 and 30 in them. From the box with 10 draw an arrow to the box with 20, and from 20 an arrow to 30. Put a slash in the arrow slot of the box with 30, meaning null. Write head under the first box. That drawing is the whole data structure, and the picture below shows it the same way.
Each node is a value box with a small link box beside it. The arrow is the next reference; the slash in the last link box means next is null.
- head
- node holding 10
- tail
- node holding 30
- size
- 3
1head refers to the node holding 10. Its next refers to the node holding 20, whose next refers to 30, and the last node's next is null, the end of the list.
Many lists also keep a second reference called tail, pointing at the last node, and a size counter. They are not needed to make it a linked list, but tail makes adding at the end fast, as we will see. Notice what is not there. There is no index 0, 1, 2 stored anywhere. A node's position is only how many arrows you follow from head to reach it.
Your turn
Solve it before you read on. Above is one example with the result expected for it. If the question leaves something open, such as an empty input or repeated values, note what you would ask the interviewer. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How the list sits in memory, compared with an array
To see why anyone would want a train instead of something simpler, compare it with an array. An array is a row of seats in a cinema, all side by side in one block of memory. If you know where the row starts and how wide a seat is, you can find seat 7 at once by arithmetic, without looking at seats 0 to 6. That is why reading a[i] takes the same short time for any i, which we write O(1), read as constant time.
A linked list gives up that arithmetic. Its nodes are created one at a time with new, and Java may place each one wherever there is free space, so the node holding 20 may sit far from the node holding 10. The only way to find the third node is to start at head and follow next twice. Reaching position i takes i hops, so it costs O(i), and in the worst case O(n) for a list of n nodes.
get(2) cannot jump to the third node. It starts at head and follows next twice, one hop per step.
- i
- 0
- cur
- node holding 10
1
get(2)starts with cur on head, the node holding 10. The node we want is two hops away, and the only way there is along the arrows.- i
- 1
- cur
- node holding 20
2The loop runs cur =
cur.nextonce, so cur moves to the node holding 20. One hop done, one to go.- i
- 2, loop stops
- cur
- node holding 30
- returns
- 30
3A second cur =
cur.nextreaches the node holding 30. i is now 2, the loop stops, and get returns 30.
So a beginner fairly asks why we would ever accept that. The answer is what the train gains in return. Adding a coach at the front, or unhooking one from the middle, only changes one or two couplings. No other coach moves. In an array, inserting at the front means every seat-holder must shuffle one seat to the right, n moves for n values. A linked list trades fast reading by position for cheap changes to its shape.
3Singly, doubly and circular lists
The list we drew, with one next reference per node, is called a singly linked list. Its weakness is that it only goes forward. Standing on the node holding 20, there is no way back to 10 except starting again at head. A doubly linked list fixes that by giving each node a second reference, prev, to the node before it, so we can walk both ways and unhook a node using only a reference to it.
A circular list is the third variant. Its last node's next points back to the first node instead of null, so walking never falls off the end. It suits things that go round in turns, such as giving each player a turn in a game. The table sets the three side by side.
| Kind | Each node holds | Can walk | Typical use |
|---|---|---|---|
| Singly linked | value, next | forwards only | stacks, simple queues, interview problems |
| Doubly linked | value, prev, next | both ways | java.util.LinkedList, LRU caches |
| Circular | value, next (last points to first) | round and round | round-robin turns, ring buffers |
The extra prev reference has a cost. Each node is bigger, typically by 4 or 8 bytes in Java, and every insert or remove must update twice as many links, which means twice as many chances to forget one. For most interview problems the singly linked list is what you are given, and the challenge is working with only next.
4How the basic operations work, by hand
Let us work the three common operations on paper before any Java, using the train. Adding at the front is the easiest. Make a new coach, couple it to the current engine, and call the new coach the engine. In list terms, create a node whose next is the current head, then set head to the new node. Nothing else changes, so it is O(1) whatever the length.
Adding at the end is O(1) too, but only if we keep tail. We couple the last coach to the new one, tail.next = newNode, and then move tail onto it. Without tail we would first have to walk the whole train to find the last coach, which is O(n). This is why our list keeps that second reference.
Removing a value is the operation that teaches the most. To unhook coach 20, we must tell the coach before it, 10, to hold on to 30 instead. So we walk with two fingers, prev on the node before and cur on the node we are looking at. When cur holds the value, prev.next = cur.next makes the list skip it. Nothing refers to the skipped node any more, so Java's garbage collector, the part of Java that frees objects nobody can reach, reclaims it. The link change is O(1), but finding the node first is O(n).
The frames follow main: addLast(20), addLast(30), addFirst(10), remove(20), remove(30), addLast(40). A dimmed node is no longer reachable from head.
- op
- addLast(20)
- head
- 20
- tail
- 20
- size
- 1
1
addLast(20)on an empty list. tail is null, so the new node becomes both head and tail. size is 1.- op
- addLast(30)
- head
- 20
- tail
- 30
- size
- 2
2
addLast(30). tail is the node 20, sotail.next= n links 20 to the new node, then tail moves onto 30. No other node is touched.- op
- addFirst(10)
- head
- 10
- tail
- 30
- size
- 3
3
addFirst(10). The new node is made with next = the old head, 20, and then head moves onto it. Nothing had to shift.- op
- remove(20)
- prev
- 10
- cur
- 20, a match
4
remove(20)walks with prev and cur. cur starts on 10, which is not 20, so prev moves to 10 and cur to 20, the node to remove.- op
- remove(20)
- 10 now points
- to 30
- size
- 2
5
prev.next=cur.nextmakes 10 point straight to 30. Node 20 still points to 30, but nothing points to it, so the list skips it.- list
- [10 -> 30]
- head
- 10
- tail
- 30
- size
- 2
6With no reference left to it, node 20 is cleared away by the garbage collector. The list reads 10 -> 30.
- op
- remove(30)
- head
- 10
- tail
- 10, moved back
- size
- 1
7
remove(30)finds 30 after 10, so 10's next becomes null. cur was the tail, so tail = prev moves tail back onto 10.- op
- addLast(40)
- list
- [10 -> 40]
- head
- 10
- tail
- 40
- size
- 2
8
addLast(40)links the tail, 10, to a new node and moves tail onto it. Had we forgotten to move tail back, 40 would hang off the removed node.
Two special cases hide inside remove, and they are where beginners slip. If the value is in the head, there is no node before it, so prev is null and we move head itself to head.next. And if the node removed was the tail, tail must move back to prev. Forgetting that second fix is the most common bug in hand-written lists, and it is tempting to forget because the list still prints correctly. The damage only shows on the next addLast, which would link the new node to a node no longer in the list. The last two frames of the picture show exactly this case.
5A small linked list in Java
Here is a minimal singly linked list of ints with a head, a tail and a size. The nested Node class is private, because people using the list should see values, never nodes. main runs the same operations as the picture and prints the list along the way, and each comment shows what that line prints.
Let us read it in the order main uses it. addLast(20) runs first. It creates a node with next null. tail is null because the list is empty, so both head and tail are set to the new node. addLast(30) finds tail is not null, so it runs tail.next = n and then tail = n, the two steps we did on paper. addFirst(10) does everything in one line, head = new Node(v, head), which builds the node with the old head as its next and then moves head onto it. The check if (tail == null) only matters when addFirst is the very first call.
get(index) starts cur at head and runs cur = cur.next index times. For get(2) that is two hops, so it prints 30. There is no shortcut, which is why the comment says O(n). If index were 3 or more, cur would become null and cur.value would throw a NullPointerException; a full implementation checks the index first.
remove(v) is a for loop that moves prev and cur together, prev = cur, cur = cur.next, so prev is always one node behind. When cur.value matches, the line if (prev == null) head = cur.next; else prev.next = cur.next does the unhooking and covers the head case. The next line, if (cur == tail) tail = prev, is the fix that keeps tail honest. Then size goes down and we return true. If the loop reaches null without a match, the value was not there and we return false.
The last lines of main test that fix on purpose. remove(30) takes away the tail, and addLast(40) must then attach to 10. It prints [10 -> 40], which proves tail moved back correctly.
6Linked list versus array, and java.util.LinkedList
Now that we have both pictures, cinema seats and train coaches, we can compare them fairly. Java's ArrayList is a growable array, so it reads any position in O(1) but must shift elements to insert or remove in the middle. A linked list is the opposite. The table puts the main operations side by side.
| Operation or property | Array or ArrayList | Linked list |
|---|---|---|
| Read element i | O(1), direct address | O(n), follow i links |
| Insert or remove at the front | O(n), shift everything | O(1) |
| Insert or remove at the end | O(1) amortised | O(1) with a tail reference |
| Insert or remove given a node | O(n), shift | O(1) if doubly linked |
| Memory per value | the value only | value plus one or two references |
| Walking in order | fast, values side by side | slower, nodes scattered |
In practice ArrayList wins more often than the table suggests, and it is worth knowing why. A processor reads memory in blocks called cache lines, so when it reads one array element, the next few are usually fetched with it for free. Walking a linked list jumps between scattered nodes and loses that help. Each Java node is also a separate object with its own header, so a list of a million ints can use several times the memory of an int array.
Java's built-in java.util.LinkedList is a doubly linked list with head and tail references. It works both as a List and as a Deque, a queue you can use at both ends, so addFirst, addLast, removeFirst and removeLast are all O(1), and get(i) walks from whichever end is closer. For most code ArrayList is the better list and ArrayDeque the better queue. Linked lists earn their place when we already hold a reference to a node and must remove or move it in O(1), as in an LRU cache, which pairs a HashMap from key to node with a doubly linked list.
7How to recognise problems that need linked list thinking
Linked list questions look varied at first, but a few clues cover most of them. The first clue is the input itself. When a problem hands you a head node and expects O(1) extra space, the work is rewiring next references in place while keeping a finger on every node you must not lose. Reversing a list, merging two sorted lists and removing duplicates all have this shape, and the question to ask before every change of next is which node you would lose sight of.
The second clue is a question about position or shape when you do not know the length: the middle node, the k-th node from the end, whether the list loops back on itself, or where two lists meet. These point to two pointers that move at different speeds or start at different places, such as a slow pointer moving one node per step and a fast one moving two.
The third clue is a design question that needs O(1) removal of an item you already hold, or O(1) moving of an item to the front. That points to a doubly linked list paired with a HashMap, as in the LRU cache. If you can tell which of these shapes a question has, you have usually done half the work.
8Follow-up questions
Why do linked list solutions often start with a dummy node?
Many operations treat the head differently, because it has no node before it, just as remove needed a special case when prev was null. A dummy node is a spare node placed in front of the real head, holding no meaningful value. With it, every real node, including the first, has a node before it, so the special case disappears. At the end we return dummy.next as the real head. It costs one extra object and removes a whole family of bugs.
How do you find the middle of a linked list in one pass?
We use two references, slow and fast, both starting at head. In each step slow moves one node and fast moves two. When fast reaches the end, it has walked twice as far as slow, so slow is in the middle. On 10 -> 20 -> 30 -> 40 -> 50, fast goes 10, 30, 50 while slow goes 10, 20, 30, and slow stops on 30. The loop runs while fast != null and fast.next != null, which handles both odd and even lengths.
Is a linked list ever faster than an ArrayList in real Java code?
Yes, but in fewer places than the textbook table suggests. When you repeatedly insert or remove at the front, or remove items through an iterator while walking, a linked list avoids the shifting an ArrayList must do. For most other work, reading by index or walking in order, the ArrayList wins because its values sit together in memory. When unsure, measure with realistic data.
9Conclusion
A linked list is a train of nodes, each holding a value and one coupling to the next, reached from the head and ending in null. It gives up reading by position in O(1) and gains O(1) changes to its shape wherever we already have a finger on the right node, and keeping head, tail and size correct through every change is most of the craft. The clue to remember is a question that hands you nodes and asks you to change their order or find a place without knowing the length: think in arrows, and before changing any next, keep a reference on every node you would otherwise lose.
Open with
A linked list stores a sequence as separate node objects. Each node holds a value and a reference to the next node, the list is reached through the head, and the last node's next is null.
Then, if they want more
Because nodes are not stored side by side, reading position i means following i links, which is O(n), unlike an array's O(1). In return, inserting or removing at a place I already hold a reference to is O(1), because only a link or two changes and nothing shifts.
Adding at the front is always O(1), and adding at the end is O(1) if I keep a tail reference. Removing by value needs a walk with a previous pointer, and I must handle removing the head and fix the tail when I remove the last node.
A doubly linked list adds a prev reference so I can walk backwards and unhook a node directly, which java.util.LinkedList does.
In practice ArrayList is usually faster because of cache locality and lower memory, so I reach for a linked list when I need O(1) splicing, as in an LRU cache with a HashMap, or when a problem gives me nodes to rewire in place.
About this answer
- Last updated
- 15 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 32 times in that collection. How it is counted
- Official documentation
- StringBuilder (Java 21), java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a linked list, and how is it structured?": https://scrivio-interview-library.netlify.app/data-structures/solutions/what-is-a-linked-list-and-how-is-it-structured-68849b/