# Thread: Creating a reverse List<Integer> using recursion - HELP!

1. Member
Join Date
Apr 2014
Posts
6
Rep Power
0

## Creating a reverse List<Integer> using recursion - HELP!

Can someone help me?
I created this code, seems like it should be working properly and the code is right, but the output is not what it should be.
The action gets a List<Integer> and should make it reversed.
As example: 1 2 3 4 5 ---> 5 4 3 2 1
The Nodes in the new list (lst2) should be in a reversed position.
The code does compile with no errors.
Note: I do have Node<T> and List<T> classes

Java Code:
```public class NodeReverse
{
public static void reverse (List<Integer> lst, Node<Integer> node, int counter, List<Integer> lst2, Node<Integer> pos)  // Should make lst = lst2 while lst2 is the opposite to lst
{
node = node.getNext();
counter++;
if(node.getNext()!=null)
reverse(lst, node, counter, lst2, pos);
pos = lst2.insert(pos,node.getInfo());
if(counter==0)
lst = lst2;
}
public static final int Nfor=5, Nnode=10;
public static void main(String[] args)
{
Node<Integer> node = new Node<Integer>(Nnode);
Node<Integer> temp = node;
for(int i=0 ; i<Nfor ; i++)  // Setting a new Node<Integer> chain
{
Node<Integer> next = new Node<Integer>((Nnode+i+1));
temp.setNext(next);
temp=temp.getNext();
}
List<Integer> list = new List<Integer>();
Node<Integer> pos = list.getFirst();
for(int i=0 ; i<=Nfor ; i++)  // Inserts the Node<Integer> chain into the List<Integer> 'list' and presents it
{'
int info = node.getInfo();
pos = list.insert(pos,info);
System.out.print(info+"  ");  // Prints: "10  11  12  13  14  15"
node = node.getNext();
}
System.out.println();
List<Integer> list2 = new List<Integer>();  // Creates an empty List<Integer>
Node<Integer> rNode = list.getFirst();
int counter=0;
reverse(list, rNode, counter, list2, null);  // Should make the List<Integer> 'list' equals to List<Integer> 'list2'
pos = list.getFirst();
for(int i=0 ; i<=Nfor ; i++)  // Presents the new-old List<Integer> 'list'
{
int info = pos.getInfo();
System.out.print(info+"  ");  // Prints the same: "10  11  12  13  14  15"
pos = pos.getNext();
}
}
}```
The output:
10 11 12 13 14 15
10 11 12 13 14 15

The output I need:
10 11 12 13 14 15
15 14 13 12 11 10

I think the problem is the part where I typed: lst = lst2;

HELP!
Last edited by Ben Bublil; 01-02-2015 at 01:56 PM.

2. ## Re: Creating a reverse List<Integer> using recursion - HELP!

The posted code doesn't compile without errors.
Please fix the errors and make sure all class definitions are provided.

Also add some comments describing what the statements in the code are trying to do.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Creating a reverse List<Integer> using recursion - HELP!

There must be more to this than just what you stated in the title. Why do you have a class Node? And since you can't instantiate an interface that code could not possibly be correct.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•