Quick revision
The short answer
Deleting a node when we hold only that node is a linked list problem with a twist: the previous node is out of reach, so we change the node we have instead. We copy the successor's value into the given node and set node.next = node.next.next, which removes the successor, in O(1) time and O(1) space. The trick cannot delete the tail, and it changes which node object holds which value.
The solution
What it costs
| Approach | Time | Needs | Can delete the tail? |
|---|---|---|---|
| Find the previous node from the head | O(n) | the head | yes |
| Shift every later value back | O(n) | only the node | no |
| Copy the successor and skip it | O(1) | only the node | no |
| Doubly linked list, use prev | O(1) | a prev reference | yes |
What to remember
We deleted a node from a singly linked list while holding only that node. Since the previous node is out of reach, we copy the successor's value into our node and point our node past the successor, which removes the successor and makes the list read as if our node were gone, in O(1) time and space. The tail cannot be deleted this way, and node identities shift. The clue to remember is a link you would need to change that is out of reach: change something you can reach, here the data, so the list looks the same from the head.
1What it means to delete a node when we only hold that node
Think of a treasure hunt laid out with clue cards. Each card has a message on it and says where the next card is hidden, and the organiser keeps only the first card. Suppose a friend hands you one card from the middle of the hunt and asks you to remove it, but you have no idea where the card before it is, the one that sends players to yours. You cannot change that earlier card. What can you do? You can change your own card, and you can read the card it points to. Hold on to that thought, because it is the whole trick.
A singly linked list is that treasure hunt. Each node holds a value and a reference called next to the node after it, and the list is normally reached from its first node, the head. The usual way to delete a node X is to find the node P before it and set P.next = X.next, so that walking the list skips X. In this problem we are given only a reference to X, not the head, and no node knows the one before it, so P is out of reach.
Let us keep one example, the list 4 -> 5 -> 1 -> 9, where we are handed the node holding 5 and must make the list read 4 -> 1 -> 9. Try it by hand on paper. Draw four boxes with arrows. Put your finger on the 5. You are not allowed to touch the box with 4 or its arrow. Now rub out the 5 and write in the value of the next box, 1, so the boxes read 4, 1, 1, 9. Then redraw your box's arrow so it jumps over the next box straight to 9. Read from the head: 4, 1, 9. It worked, without touching the 4.
The list 4 -> 5 -> 1 -> 9. We are handed a reference to the node holding 5, and nothing else.
- given
- node (holds 5)
- not given
- head, the node before
- expected
- 4 -> 1 -> 9
1We are given only node, a reference to the node holding 5. We cannot see the head or the node holding 4, yet the list must end up reading 4 -> 1 -> 9.
What to assume
- Can the given node be the last node, the tail?
- The question normally promises it is not; we throw an exception if that promise is broken.
- Is the list singly linked?
- Yes; with a reference to the previous node the problem would disappear.
This problem looks impossible at first, and that is exactly why it is asked. Notice how little we did by hand: one value copied and one arrow moved. There is no loop and no search. The only real difficulty is believing that changing values, rather than links, is allowed, and understanding what it costs.
A careful candidate asks a few questions first. Can the given node be the last node, the tail? The question normally promises it is not, and we will see why that promise is essential; we throw an exception if it is broken. Is the list singly linked? Yes; with a reference to the previous node the problem would disappear. Do other parts of the program hold references to these nodes? That matters for one subtle cost we will meet at the end.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. 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 to spot the pattern when the pointer you need is missing
A beginner reading this question usually reaches for the textbook deletion, find the previous node and rewire its next. That is the right habit for linked lists, so it is natural to look for a way to reach the previous node. The usual tricks for that, walking from the head with two references, need the head, and we do not have it. So let us read the question again, slowly, and take its clues one at a time.
The first clue is the phrase given only a pointer to that node. A pointer, in Java, is a reference variable that names a node, like a finger on one card. The question is telling us exactly what we can reach: this node, and by following next, everything after it. Nothing before it.
The second clue is the word singly, hidden in the setting. Because links point only forward, the node before ours is unreachable, and the link we would normally change, P.next, belongs to that node. When the link we would need to change is out of reach, we must change something we can reach instead.
Together these clues point to the linked lists family, and to a particular move inside it: copying data instead of rewiring the missing link. In linked list problems there are only two kinds of change, changing the next references or changing the values stored in nodes. When the first is blocked, ask whether the second can produce the same list as seen from the head. That is the rule to carry to new problems, along with its warning that the node objects then no longer hold the values they used to.
Some patterns look similar but do not fit. A dummy node in front of the head helps when the head itself might be removed, but we have no head to put it in front of. Fast and slow pointers find middles and cycles, which is not the question. A doubly linked list, where each node also knows the one before it, would solve the problem directly, which is worth saying, but it changes the data structure we were given.
3The honest first attempt, a brute force that shifts every later value
The first idea that works with only the node is a brute force that makes the list read as if every later value had moved one place towards the front. Copy the next node's value into ours, step forward, copy again, all the way to the second to last node, and then drop the last node. It uses only what we can reach, and it is correct.
Shifting every later value back one place. The second frame already shows the list one step from the answer.
- reads as
- 4 -> 5 -> 1 -> 9
1The brute force starts on the node holding 5. Its plan is to pull every later value one place towards the front, then drop the last node.
- reads as
- 4 -> 1 -> 1 -> 9
2It copies 1 into our node. Look at the list now: 4 -> 1 -> 1 -> 9. Only one thing is wrong, a spare 1, and every later node is already right.
- reads as
- 4 -> 1 -> 9 -> 9
3It carries on anyway, copying 9 into the next node, so the list reads 4 -> 1 -> 9 -> 9. This copy, and any like it, was work we did not need.
- reads as
- 4 -> 1 -> 9
4Finally it cuts the old tail off, and the list reads 4 -> 1 -> 9. Correct, but it touched every node from ours to the end.
Let us count its work on the example. Our node is the second of four. It copies 1 into our node, copies 9 into the next, and then cuts the last node off, so it touched three nodes. For a node at position k in a list of n nodes, it touches n - k nodes, so in the worst case, when the given node is near the head of a long list, the time is O(n), read as order n. The extra memory, called the space, is O(1), since it uses no extra structure.
So where is the waste? Look at the second frame of the picture. After the very first copy the list reads 4 -> 1 -> 1 -> 9, and the only thing wrong is one spare 1. Every node after it is already exactly right. All the shifting that follows is effort spent fixing something that was never broken.
So the question that leads further is this. After that first copy, can we get rid of the spare 1 directly, instead of shifting everything behind it?
4Getting to the constant time solution, one observation at a time
The first observation is about the spare 1. It sits in the node right after ours, which we call the successor. And unlike the node before ours, the successor is something we can remove properly, because the link that points to it is our own node's next, which we are allowed to change.
So the method is two lines. node.val = successor.val copies the successor's value into our node, and node.next = successor.next makes our node point past the successor. The list now reads 4 -> 1 -> 9 from the head. We did not really remove the node we were given; we removed its successor and moved the successor's value forward one place. To anyone walking the list from the head, the two are indistinguishable.
Here is the question a beginner asks. Why not simply write node = null, or node = node.next, and be done? Let us try it and see what happens. In Java, node is a local variable inside our method, a copy of the caller's reference. Setting it to null changes only that local copy. The node holding 4 still points to the node holding 5, so the list from the head still reads 4 -> 5 -> 1 -> 9. Changing a variable is not the same as changing the list; only changing fields of the nodes, such as val and next, affects what others see.
That same fact explains why the tail cannot be deleted this way. The tail has no successor to copy from, so the first line has nothing to read, and setting our local variable to null would not change the node before the tail, which still points at it. The method therefore checks node.next == null and throws an exception rather than silently doing nothing, which would hide a bug in the caller. This is the case beginners most often forget, and it is tempting to forget because the trick works so neatly everywhere else.
A third line, successor.next = null, is not needed for the list to be correct. It cuts the removed node's own arrow, so that any code still holding the old successor cannot walk back into the list through it. It is good tidiness, and it costs nothing.
Why is it correct? Before the call, the list read from the head is the values before our node, then our value v, then the successor's value w, then the rest. After the call, our node holds w and points to the rest, so the list reads the values before our node, then w, then the rest, which is exactly the list with v removed. Nothing before our node was touched, and nothing after the successor was touched.
5Watching the two deletions on the example
The picture follows main on 4 -> 5 -> 1 -> 9. The nodes stay in the places they were created, so you can see which node object is which. Frames two to five delete the node holding 5, one line of code per frame, and frames six to nine then delete the head in the same way. Removed nodes fade out; the panel shows how the list reads from the head.
Watch for two things as it plays. The values change before the arrows do, and the node we were given never leaves the list.
Nodes stay where they were made. Faded nodes are no longer reachable from the head; the panel shows how the list reads.
- reads as
- 4 -> 5 -> 1 -> 9
- step
- start
1We start with 4 -> 5 -> 1 -> 9 and a reference, node, to the node holding 5. We cannot reach the node holding 4, so we must work forward.
- reads as
- 4 -> 5 -> 1 -> 9
- step
- successor
2The first line names the node after ours, successor =
node.next, the node holding 1. It is the node we will really take out of the list.- reads as
- 4 -> 1 -> 1 -> 9
- step
- copy
3
node.val=successor.valcopies 1 into our node. For a moment the list reads 4 -> 1 -> 1 -> 9, with the value 1 twice.- reads as
- 4 -> 1 -> 9
- step
- skip
4
node.next=successor.nextmakes our node point straight at 9. Walking from the head now skips the old successor, and the list reads 4 -> 1 -> 9.- reads as
- 4 -> 1 -> 9
- step
- clear
5
successor.next= null cuts the removed node's own arrow, so anyone still holding it cannot walk back into the list. The first deletion is done.- reads as
- 4 -> 1 -> 9
- step
- successor
6main now calls
deleteNode(head), on the node holding 4. Its successor is the node holding 1, the very node we changed a moment ago.- reads as
- 1 -> 1 -> 9
- step
- copy
7We copy 1 into the head node, so the list reads 1 -> 1 -> 9. The caller's head variable still refers to this same node object.
- reads as
- 1 -> 9
- step
- skip
8The head node now points straight at 9, skipping its old successor. The list reads 1 -> 9 when walked from head.
- reads as
- 1 -> 9
- step
- clear
9We cut the removed node's arrow. Two nodes have left the list, the list reads 1 -> 9, and head did not have to change at all.
Let us read the first deletion back. In the third frame our node already holds 1, and the list briefly shows 1 twice; this is the moment from the brute force picture. In the fourth frame one arrow moves, our node now points at 9, and the old successor fades out, because walking from the head no longer reaches it. In the fifth frame its own arrow is cut.
The second deletion shows why deleting the head works too. The head has a successor, so we copy its value, 1, into the head and skip the successor. The caller's head variable still refers to the same node object, which is why main can print the list from head afterwards without changing head at all. The list reads 1 -> 9.
6The complete Java program
Here is the full program, with a small Node class, the deleteNode method, and two helpers: of builds a list from values and show prints one. main deletes the node holding 5, then the head, then tries the tail, and the comment on each printing line shows what it prints.
Let us read deleteNode in the order it runs. First the if statement rejects a null reference and the tail, throwing an IllegalArgumentException with a clear message, because neither can be handled correctly. Then successor = node.next gives a name to the node we will really remove.
node.val = successor.val copies its value into our node, node.next = successor.next unlinks the successor by pointing past it, and successor.next = null tidies the removed node. Three field writes, no loop.
In main, of(4, 5, 1, 9) builds the list and five = head.next picks out the node holding 5, the only thing deleteNode receives. The first show prints 4 -> 1 -> 9. deleteNode(head) then works on the head, printing 1 -> 9. Finally head.next is the node holding 9, the tail, so the call throws, and the catch block prints the message instead of letting the program stop.
7How much it costs, and the limits of the trick
Let us count on the example first. The first deletion did one check, one read and three field writes, and so did the second, whatever the length of the list. So the time is O(1), read as constant, and the space is O(1). The brute force touched three nodes here and up to n in general, and the textbook deletion would need O(n) to walk from the head to the previous node.
| Approach | Time | Needs | Can delete the tail? |
|---|---|---|---|
| Find the previous node from the head | O(n) | the head | yes |
| Shift every later value back | O(n) | only the node | no |
| Copy the successor and skip it | O(1) | only the node | no |
| Doubly linked list, use prev | O(1) | a prev reference | yes |
The edge cases are few. The tail and a null reference are rejected with an exception. The head works, as we saw. Duplicate values cause no trouble, because we copy and skip by reference, never by searching for a value.
The deeper limit is identity. After the call, the node object we were given holds a different value, and the object that used to hold that value has left the list. If other code kept a reference to the node holding 1, perhaps in a map from values to nodes, that reference now points at a removed node. In a real system where nodes carry large records, or are referenced from elsewhere, that can be a real bug, and it is worth mentioning to the interviewer.
8Follow-up questions
How would a doubly linked list change this?
In a doubly linked list each node also has a prev reference to the node before it, so the missing node is no longer missing. We delete X properly with X.prev.next = X.next and X.next.prev = X.prev, taking care when X is the first or the last node, where one of those is null. No values move, the tail can be deleted, and references held elsewhere stay valid. That is why Java's LinkedList is doubly linked.
Why not set the given node to null to delete it?
Because node is only a local variable inside our method, a copy of the caller's reference. Setting it to null forgets our copy, but the node before still holds its own reference to the same object, so the list is unchanged. Only writing to a node's fields, val or next, changes what everyone walking the list sees.
Is there any way to delete the tail if we are given only the tail?
Not with a singly linked list and nothing else. The only reference to the tail that matters is the next of the node before it, and that node is unreachable from the tail. Some designs replace the tail with a special sentinel node marked as empty, so the trick always has a successor to copy, but that changes the list's design rather than solving the original problem.
9Conclusion
We deleted a node from a singly linked list while holding only that node. Since the previous node is out of reach, we copy the successor's value into our node and point our node past the successor, which removes the successor and makes the list read as if our node were gone, in O(1) time and space. The tail cannot be deleted this way, and node identities shift. The clue to remember is a link you would need to change that is out of reach: change something you can reach, here the data, so the list looks the same from the head.
Open with
Let me confirm the setup. I get a reference to a node in a singly linked list, but not the head, and the node is promised not to be the tail.
Then, if they want more
Normally I would set the previous node's next past it, but I cannot reach the previous node. Shifting every later value back one place works, but it is O(n) and does unnecessary work, because after the first copy everything behind is already right. So I copy the successor's value into the given node and set node.next to successor.next.
That removes the successor, and from the head the list reads as if the given node were deleted. I also set successor.next to null, which is tidy but not required. Setting the parameter itself to null would do nothing, because it only changes my local copy of the reference.
For 4, 5, 1, 9, deleting the 5 gives 4, 1, 9. It is O(1) time and O(1) space. Two limits are worth stating. The tail cannot be deleted this way, so I throw an IllegalArgumentException rather than fail silently.
And node identities change: the object I was given now holds the next value, which matters if other code holds references to these nodes. With a doubly linked list I would delete properly using prev.
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 "Delete a node from a linked list when you are given only a pointer to that node.": https://scrivio-interview-library.netlify.app/data-structures/solutions/delete-a-node-from-a-linked-list-when-you-are-given-only-a-point-0aa8aa/