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:
Code:
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.