| You are here: DEVPPL ‹ Forum ‹ Programming ‹ Java Forum |
NOTIFICATIONS
|
|
|||||||||||||||
Login |
Reversing a linked structure recursively
1 post
• Page 1 of 1
0
Reversing a linked structure recursively
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:
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.
- 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
|
|