Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Recursion isn't hard, it's just not taught correctly. It's fairly easy to break it down into rules.

Because they are shown examples, instead of taught how, people memorize how to do stuff, instead of learning it from principles.

Take the classic question, recursively reverse a linked list.

You will see a lot of people making false starts, or trying to reverse the list in some way like they would reverse an array, or even repeatedly walking the list to the end.

But if you understand some simple rules about recursion, it's fairly easy to figure out how to do this on the fly. And that is so much better then memorization, because you can explain why you are doing things.

first you are reversing, so you're moving backward, which means you calls your function, then do assignments. If you were doing something moving forwards, like printing in order, you'd print first, then call the function.

Now we need to worry about the end, and the beginning.

First, the beginning, which happens after we have recursively walked the list.

Now, we want to walk to the last node before null, and we need a place to store what we return. so assuming we have something like this in c 'node * recursivereverse(node * current)' and a node structure with next as the next link (we never touch our data)

our test is if (current->next) node * newbeginning = recursivereverse(current->next); else return current; //some assignments go here;

return newbeginning;

Then we just need to figure out the assignments.

Now, people may get tripped up and try to use the new beginning... but then they have to walk it to the end... but the current node already knows the node that comes after it.

so we assign current->next->next = current; IE the node that was previously after the current node now points to the current node as it's next. And that's fine for all the rest of the list, until we get to our original beginning node. We need to worry about the new end.

Since it's going to the end, we need to point it to NULL, otherwise it will point to it's predecessor, meaning we'll have an infinite loop at the end of the last two nodes.

Easy way to deal with it is to just assign the next of the current node to NULL, for each node.

so we add to current->next->next = current; current->next = NULL;

and then you have a reversed linked list. This makes certain assumptions about the list (like it has at least one node, and isn't circular), so don't just copy this for your interview.

So with some simple rules, I figured out how to reverse a linked list. But those simple rules aren't taught, or at least, I came up with them for myself after I became a computer science tutor at my college, because I needed people to understand recursion.

So I guess my point is, it's not that people can or can't understand recursion, it's whether they are taught to think about things in the right way.

Now to a certain extent, we need to teach people to generalize, but if you teach people a general rule (always worry about the beginning and end of any data structure, and that's important for everything, including memory management and debugging) and a recursion specific rule (call the function first to reverse, call the function after to use the current order), and they can generalize.

Too often people come to program with the ability to copy code and get it working (and that isn't anything to denigrate, because it's not simple), but lack the understanding of how to think about things in a deeper way.



Because it was lunchtime and I wanted to make sure I was still employable...

static MyLinkedList<T> ReverseLinkedList<T>(MyLinkedList<T> linkedList) { if (linkedList.Next == null) return linkedList;

  var newTail = linkedList.Next;
  var reversedList = ReverseLinkedList(linkedList.Next);
  newTail.Next = linkedList;
  linkedList.Next = null;

  return reversedList;
}


Agreed about recursion being taught the wrong way. Recursion is implicitly fundamental to learning basic arithmetic (what is 5? it's just (1 + (1 + (1 + (1 + (1 + 0))))). Reasoning by induction is just recursion. I think children have a recursive intuition for counting (at least from watching my 3 year old) that quickly gets squashed when they start learning 'math' in school.


I've always thought of reversing a linked list in a visual way.

In my mind, a link list is a bunch of nodes with arrows pointing right. To reverse the list, all I need to do is point the arrows to the left.

6(ish) lines of code later, and I've got a recursive reverse method.


Replying so that i can go back to this and understand it one day


Mark, when you come back to this, this might be helpful for you. https://github.com/gryftir/recursivereverse


Thanks! Sorry for asking this, but why the strange (to me) indent?


8 spaces?

Probably because this is similar to the Linux kernel programming style. Many of us learnt C from the kernel or projects that follow it's coding style.


Assuming you mean the code on github, I use tabs for indentation in vim, but have tabstop and setwidth set to 2. So if the code has 8 spaces, that is github or the browser. Feel free to make a pull request, or I can try to fix it (though I am kinda focused on my portfolio for internship applications atm). Should be a simple regex though.


  perl -pi -e 's/^(\t+)/"  " x length($1)/e' recurse.c
seems to do the trick; vary the number of spaces inside the "" to taste.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: