# LinkedList in reverse order recursion

Page 1 of 1

## 4 Replies - 1192 Views - Last Post: 28 December 2012 - 12:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305056&amp;s=10b7b51a1a725b52bba4a31d2c5e9809&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 RourkeOkeffe

Reputation: -2
• Posts: 89
• Joined: 27-October 12

# LinkedList in reverse order recursion

Posted 28 December 2012 - 09:50 AM

Hey Guys I'm revising for a test.The question asks as follows to complete the method.That prints a singly linked list backwards.Using the printBackward method which takes the length of the list as a parameter.
Once you have completed the method create a simple program to print the list forward and then in reverse order using the
printBackward method

I am not sure how to implement this is what I have so far any help would be greatly appreciated.

import java.util.ListIterator;

@SuppressWarnings("serial")

public void printBackward(Integer n) {

if (n > 0) {
ListIterator<E> itr = listIterator();
int count = 0;

while (itr.hasNext()) {
//Block 1

}
//

} else {
//Block 3
}

}// end method

public static void main(String[] args) {

System.out.println("myList: "+myList);

//printBackward(0);

}
}// end class

Is This A Good Question/Topic? 0

## Replies To: LinkedList in reverse order recursion

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,170
• Joined: 27-December 08

## Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 10:27 AM

See the ListIterator documentation. You could iterate n spaces forward, then go backwards and print out the elements. Or you could just iterate forward and store the elements on something like a Stack.

Also, use primitives instead of the wrappers whenever possible, as it is more efficient.

### #3 RourkeOkeffe

Reputation: -2
• Posts: 89
• Joined: 27-October 12

## Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 10:32 AM

It says I must have a dimension to make a linkedlist using int which i don't know why it says this.

### #4 RourkeOkeffe

Reputation: -2
• Posts: 89
• Joined: 27-October 12

## Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 11:05 AM

macosxnerd101, on 28 December 2012 - 10:27 AM, said:

See the ListIterator documentation. You could iterate n spaces forward, then go backwards and print out the elements. Or you could just iterate forward and store the elements on something like a Stack.

Also, use primitives instead of the wrappers whenever possible, as it is more efficient.

How could I adapt what i have to go the end then traverse back

public void printBackward(Integer n) {

if (n > 0) {
ListIterator<E> itr = listIterator();
int count =n;

E item;

while (itr.hasNext()) {
//Block 1

}
//

} else {
System.out.println("List is empty or end of list reached");
}

}// end method

public static void main(String[] args) {
System.out.print(myList.printBackward(myList.size()));

System.out.println("myList: "+myList);

//printBackward(0);

}
}// end class

This post has been edited by macosxnerd101: 28 December 2012 - 12:51 PM
Reason for edit:: Please use code tags

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 12:11 PM

Seems to me overcomplicated for nothing

void printBackward(int n) {
if(n < (size() - 1))
printBackward(n + 1);
System.out.println(get(n));
}

should do the trick. Just call printBackward(0);

This post has been edited by pbl: 28 December 2012 - 01:38 PM