| | |
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:
Solved Threads: 0
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:
i know this is wrong but i tried, but how do you 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:
Java Syntax (Toggle Plain Text)
public static String backwards(IntNode head) { if (head == null) { return null; } if (head.next == null) { return head; } reverse(head.next); head.next.next = head; head.next = null; return head; }
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
You want to use a StringTokenizer. So and them together.
So:
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.
So:
Java Syntax (Toggle Plain Text)
String head = "47 23 2 5 98"; String returnHead = ""; StringTokenizer s = new StringTokenizer(head, " "); while(s.hasNextToken()){ returnHead = s.nextToken() + ", " + returnHead; }
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.
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
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.
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.
•
•
Join Date: Apr 2006
Posts: 8
Reputation:
Solved Threads: 1
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.
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.
Java Syntax (Toggle Plain Text)
public static String backwards(IntNode head) { if (head == null) return null; if (head.next == null) return Integer.toString(head.item); else return backwards(head.next) + "," + Integer.toString(head.item); }
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. ![]() |
Similar Threads
- more linked list printing problems (C)
- recursive linked list (C++)
- Linked List (C++)
Other Threads in the Java Forum
- Previous Thread: The Empires (NEED HELP BAD PLEASE READ)
- Next Thread: Noob : Input Stream Trouble
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






