how do you reverse a linked list using recursion and return it as a string?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2006
Posts: 3
Reputation: modernage is an unknown quantity at this point 
Solved Threads: 0
modernage modernage is offline Offline
Newbie Poster

how do you reverse a linked list using recursion and return it as a string?

 
0
  #1
Apr 17th, 2006
How do you reverse a linked list using recursion, and return it as a string?

String backwards(IntNode head): Create and return a string containing the values in the input list, separated by commas, in reverse order. The last number should not be followed by a comma. Spacing within the string is not important. For example, if head points to the list: 12 45 -3 10 99 -47, the expression
OneListRec.backwards(head)

should return the string "-47, 99, 10, -3, 45, 12".

i am confused as to how to do this. I tried to reverse the list recursively:
  1. public static String backwards(IntNode head)
  2. {
  3.  
  4. if (head == null) {
  5. return null;
  6. }
  7.  
  8. if (head.next == null) {
  9. return head;
  10. }
  11.  
  12. reverse(head.next);
  13. head.next.next = head;
  14. head.next = null;
  15. return head;
  16. }
i know this is wrong but i tried, but how do you return it as a string ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #2
Apr 17th, 2006
Short answer: you don't.
Way too complex to achieve what you can do far more easily with just 2 commands.
Which those are I leave to you, the API documentation is your friend.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #3
Apr 17th, 2006
You want to use a StringTokenizer. So and them together.
So:
  1. String head = "47 23 2 5 98";
  2. String returnHead = "";
  3. StringTokenizer s = new StringTokenizer(head, " ");
  4. while(s.hasNextToken()){
  5. returnHead = s.nextToken() + ", " + returnHead;
  6. }
I think that is what you want.

jwenting is right you dont use recursion for this problem. When your thinking about a recursive problem you need to think is there a base case where this can end and a recursive case, which is the case that goes over and over until it reaches the base case. I attached a recursive class to this post so you can see how recursive methods work, if you're interested.
Attached Files
File Type: java Stars.java (493 Bytes, 15 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 3
Reputation: modernage is an unknown quantity at this point 
Solved Threads: 0
modernage modernage is offline Offline
Newbie Poster

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #4
Apr 17th, 2006
Oh, but I am being forced to use recursion for my homework assignment and I probably can't use StringTokenizer, they want us to learn how to implement the method ourselves. And the numbers in the list will be random numbers, not just those particular ones I wrote in. Yeah this sucks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #5
Apr 17th, 2006
Ok, yeah, so it is kind of like a permutation of the numbers.
So, think of the base case and recursive case. So the base case is when there is 1 value left, and the recursive case takes out one of the numbers.
I added some files, this is a permutation of a string, but it is very similar to what your doing. But if the way i am thinking of it, you still might need stringtokenizer.
Attached Files
File Type: java Permutation.java (1.4 KB, 25 views)
File Type: java PermutationDriver.java (489 Bytes, 7 views)
File Type: java Set64.java (1.1 KB, 11 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: eCharisma is an unknown quantity at this point 
Solved Threads: 1
eCharisma eCharisma is offline Offline
Newbie Poster

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #6
Apr 17th, 2006
Returning the reversed list as a string is even easier than actually changing the links. When you get to the end of the list, return the value as a string. On the way back, append a comma and the value of the current node as a string to the return value of backwards.
  1. public static String backwards(IntNode head)
  2. {
  3. if (head == null)
  4. return null;
  5.  
  6. if (head.next == null)
  7. return Integer.toString(head.item);
  8. else
  9. return backwards(head.next) + "," + Integer.toString(head.item);
  10. }
The trick to recursion is to let yourself go. If you think too hard about how it works or whether it's correct, you'll end up with a much more complicated solution than you need.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 3
Reputation: modernage is an unknown quantity at this point 
Solved Threads: 0
modernage modernage is offline Offline
Newbie Poster

Re: how do you reverse a linked list using recursion and return it as a string?

 
0
  #7
Apr 17th, 2006
Thank you so much, it works!! yeah i thought about it way too much and I just got even more confused..but thanks again
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC