You are here: DEVPPL Forum Programming Java Forum
NOTIFICATIONS
54.087
MEMBERS
15.684
TOPICS
62.255
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

Reversing a linked structure recursively

0

Loading

Reversing a linked structure recursively

Postby Stiks » Fri Nov 02, 2007 8:37 pm

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: Select all
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.
Stiks
 
Reputation: 0
Posts: 7
Joined: Wed Apr 18, 2007 3:25 am
Highscores: 0
Arcade winning challenges: 0

Reversing a linked structure recursively - Sponsored results

Sponsored results

Login to get rid of ads

 

^ Back to Top