Flash Games

 FAQ   Search   Members   Groups   User Control Panel      Login 

It is currently Tue Dec 02, 2008 2:00 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Reversing a linked structure recursively
PostPosted: Fri Nov 02, 2007 8:37 pm 
Offline

Joined: Wed Apr 18, 2007 3:25 am
Posts: 6
I'm attempting to reverse a linked list of integers recursively. in order to test my code, I'm doing unit test. Anyway, I can't get the list to reverse. I pretty much know why, and I have an idea of how it should work; however, I can't seem to put those thoughts into code. I think my problems start after the while loop. Here is what I have came up with so far:

Code:
public static IntNode reverseLinks(IntNode head) {
      if (head == null) {
         return null;
      } else if (head.next == null) {
         return head;
      } else {
         IntNode temp = head;
         IntNode newNode = null;

         while (temp.next.next != null) {
            temp = temp.next;
         }
         newNode = new IntNode(temp.next.data);
         temp = null;
         return new IntNode(newNode.data, reverseLinks(temp));
      }
   }


IntNode is a class with two constructors, one which takes in the value, and the other takes in the value along with the next node.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group - Flash Games
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User:
Pass:
Log me on automatically each visit: