Algorithm is one the fundamental components that a programmer should master. But every time I read a book about it, I see intrusive instructions about how to solve a problem using several algorithms. Well they just tell you that you should use this and that. Sure, they are brilliant ideas, but how do those ideas come up? If we do not know this process, it is hard to say we have really learned.
I’ve been reading the book “How to solve it” by George Pólya. It is a great book that shows you how to approach a problem using various steps. You can ask about certain questions and answer them to get a better chance to obtain the final solution. Although this book is about mathematics, I think it also applies for algorithms or even just problems in general. So I’d like to write the conversation I made myself to show the process of how we get an algorithm without giving intrusive instructions. These are just my humble ideas and suggestions are always welcome.
Q: What is the unknown? What is our goal?
A: Eh a way to delete a node in a linked list.
Q: What are the data?
A: A node and a linked list.
Q: What are the conditions and restrictions?
A: The list is singly linked. The node is in the middle of the list. And we only have access to the node to delete.
Q: What does it mean by singly linked?
A: It means the node only knows which node is the next but not the one precedes it.
Q: What does it mean by in the middle?
A: It means the node is not the head or the tail of the linked list.
Q: What does it mean by “only access to this node”?
A: Eh I guess it means we do not have access to the head of the list…only the node we want to delete is provided…man this is impossible!
Q: Why do you think it is impossible?
A: Well, to delete a node in a singly linked list, we have to know which node precedes it at least. That means we need to know the head node…
Q: Well the head is not provided. Let’s forget about the restriction and look at an example. Can you think of an example of deleting a middle node from a singly linked list?
A: Sure. Say we have a->b->c->d->e->null and we want to delete c.
Q: So what does the linked list looks like after the deletion?
Q: Is there any other way other deletion you can use to get this result?
Q: What are the differences between these two lists? (Comparison side by side!)
A: Well their lengths are different. Actually I don’t think we know what precedes c. So I would write the lists like the following:
Q: You got a point. We should only focus on things after c. Maybe I should ask a different question. Are there any differences for node d and e before and after the deletion?
A: I don’t know. Their values stay the same…wait, but their position changed after the deletion.
Q: You mentioned the value in the node. So deleting a node means…?
A: That we don’t want this value any more.
Q: Think about these two factors: value, and position shift…node can shift. What about value?
A: Eh we can shift the value too…Ah right instead of shifting the nodes…So I just need to copy the value of the nodes following c to the previous nodes and delete the last node…
Q: Yeah I know it may not come into your mind at first glance. The restrictions confine your mind. Compare the example before and after an operation. Think about different ways to achieve the same effect.
Features of a LinkedListNode: data, next node and position!
Link to the code on github (You will need the first one to run the second one):