Welcome to Dream.In.Code
Become a Java Expert!

Join 150,375 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,681 people online right now. Registration is fast and FREE... Join Now!




help me to explain this recursive code

 
Reply to this topicStart new topic

help me to explain this recursive code, reversing a linked list backwards

EquinoX
16 Feb, 2008 - 08:45 AM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2007
Posts: 63


My Contributions
here's the code:

CODE

node reverse(head)
{
if(head->next == NULL) /*last element, base case*/
{
  temp = head;
}

else
{
    temp = reverse (head->next);  /*Recursive Call on the remaining elements*/
    head->next->next = head;  /*Put the first element at the end */
    head->next = NULL;
}


what I don't understand is that after it reaches the base case,say that I have this linked list

1 --> 3 --> 5 -- > 7

the recursion goes like this

3-->5-->7

and then

5-->7

and

7

then after it reaches 7 it hits the base case, however I don't understand what head.next.next does and why's head.next == NULL. can someoene explain me this?? Thanks
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help Me To Explain This Recursive Code
16 Feb, 2008 - 09:22 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I couldn't tell you because...

A.) This is not valid Java at all. The syntax even looks more like C++
B.) It is missing a lot of things including return statements, how temp is playing a part in this and see how "head" changes (if it does).

It is an incomplete example and you could only guess what is going on based on what you have provided. Perhaps you can clean it up a bit and actually give us a good java example.

Thanks! smile.gif
User is offlineProfile CardPM
+Quote Post

EquinoX
RE: Help Me To Explain This Recursive Code
16 Feb, 2008 - 10:37 AM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2007
Posts: 63


My Contributions
QUOTE(Martyr2 @ 16 Feb, 2008 - 10:22 AM) *

I couldn't tell you because...

A.) This is not valid Java at all. The syntax even looks more like C++
B.) It is missing a lot of things including return statements, how temp is playing a part in this and see how "head" changes (if it does).

It is an incomplete example and you could only guess what is going on based on what you have provided. Perhaps you can clean it up a bit and actually give us a good java example.

Thanks! smile.gif



yeah it's actually only the pseudocode.. I am just trying to understand the concept first
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month