A few days ago, I read an ACM Queue article called Teaching Algorithmic Thinking, and it resonated with me. When learning algorithms, developing the way of thinking matters much more than memorizing the final solution. Around the same time, I was scrolling Douyin and saw @Digital Nomad Samuel explain linked-list insertion. His explanation was making almost the same point.
One Problem, Two Ways to Teach It
Take linked-list insertion. The traditional explanation often gives you two lines of template code:
# Insert a new node after head
new_node.next = head.next
head.next = new_node
But when the next problem asks you to insert at position k, insert into a doubly linked list, or insert with a sentinel node, you get stuck again because you never stopped to understand why those two lines are written that way.
Samuel's explanation was different. He first drew the linked list on a whiteboard and connected the nodes with arrows. Then he asked you to move the arrows yourself: which edge should be broken first, which one should be connected next, what happens if the order is reversed, and which part of the list disappears if you break the wrong link. Once you draw it yourself, those two lines of code become obvious.
The difference is whether code is handed to you as the conclusion or becomes the final step of a thinking process you have already walked through.
Problems I Ran Into
I made the same mistake when learning binary-tree traversal. I memorized preorder, inorder, and postorder as three separate things:
def inorder(root):
if not root:
return
inorder(root.left)
print(root.val) # inorder: visit the node in the middle
inorder(root.right)
I remembered that inorder means left, self, right; preorder moves the visit to the front; and postorder moves it to the end. That was enough to pass practice problems, but it did not transfer when I encountered graph traversal because I had never realized that all three traversals are the same underlying process.
They all recursively walk through the entire tree. The only difference is when you decide to do something with the current node. That idea takes one sentence to explain, but I spent more than half a year memorizing it as three separate tricks.
How I Learn New Things Now
I do not want to dress this up as "systems thinking" or "building a knowledge framework." For me, it comes down to three concrete changes:
- Draw first, read code later. When learning a new data structure, I start by drawing it on paper and manually simulating insert, delete, and search.
- Force myself to explain it. I can explain it to a classmate or just to the air. If I can explain it clearly, I understand it. If I get stuck at one step, that is the part I do not understand yet.
- Group problems by pattern instead of memorizing individual questions. Binary-tree traversal and graph DFS are the same kind of move. Linked-list reversal and stacks are closely related. Sliding windows and two pointers often share the same pattern. When I see a new problem, I first ask: what problem I already know does this resemble?
A Small Map for Binary Trees
mindmap
root((Binary Tree))
Basic Properties
Node Relationships
Height And Balance
Traversal Strategies
Preorder
Inorder
Postorder
Level Order
Implementation Styles
Recursion
Iteration With Stack
Typical Uses
Expression Trees
File Systems
Search Trees
Closing Thought
If you are stuck on algorithms and grinding through problems without feeling any progress, do not immediately force yourself through more of them. Find a teacher or video that explains the reasoning clearly, or draw the problem twice on a whiteboard yourself. That will often help more than grinding through ten additional questions.
References
Follow updates
Use open feeds for new articles, research, and important updates.
- Author:LeoQin
- URL:https://leoqin.com/en/article/learning-algorithm
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!