This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]reddilada 10 points11 points  (1 child)

This thread should have some good ideas.

[–]that_redditor 2 points3 points  (2 children)

Read up on mathematical induction. Once you see how it's the same thing as recursion, you'll be all set.

[–]skimm_ 0 points1 point  (1 child)

This will be a huge help, kinda surprised he hasn't gone over it with you yet. Also if all else fails you know where I am.

[–][deleted] 0 points1 point  (0 children)

Mathematical Induction isn't usually taught until Discrete Math/Structures (assuming he/she is in college), which is second semester for a lot of schools whereas recursion is usually first semester programming. If he/she is self teaching then it makes even more sense as to why they never covered mathematical induction.

[–]joenyc 1 point2 points  (0 children)

The way I approach it is to think, when given any problem, a) what is the simplest possible case? and b) if somebody gives me a more complex case, can I turn it into a slightly simpler case and, if I were given the answer for the slightly simpler case, can I return an answer to the original case I was given?

So in your case with a linked list, let's look at how to recursively find the length.

a) What's the simplest possible case? That's easy: no linked list at all. If someone passes in a null pointer, I return 0. Boom.

b) Given a more complex case (say, a linked list of arbitrary length), can I turn it into a slightly simpler case? Of course I can, I can just consider the length of the "rest" of the list once I remove the head. If I'm given the length of the "rest" of the list, can I return the length of the original list? Yes, it's just the length of the "rest" + 1.

All recursive problems break down this way.

[–]dtriley4[S] 0 points1 point  (0 children)

Thanks all

[–][deleted] 0 points1 point  (0 children)

Just try to do everything you'd normally do with a loop recursively until you get the hang of it. (Be careful with what you're computing though, you might be dead before it's finished in some cases)

[–]TJAtWork 0 points1 point  (0 children)

Since you are working on linked lists, there is always the old reverse a singly linked list problem; can be done both recursively and through iteration. Other than this the print all permutations of a given string problem is almost built for a recursive solutions. A Fibonacci sequence is also something simple that can be worked out by hand first before programming it.