Write a recursive void method to display a single-ended singly linked list backwards
I need some help with this problem. my instructions are "Write a recursive void method to display a single-ended singly linked list backwards. "
i am using custom code for links and a linked list that comes from my textbook. the textbook is "Data Structures and
in my code i made a method called recursiveReverse() which takes two arguments a integer count and an integer sizeOfList is supposed to use a for loop to get the method to where it needs to go on the list using a current and previous link. the current is where the method is at currently and the previous is where the current was before the list iterated. i am having trouble finding a way to get the method to end when the code finally gets to where it needs to be. the code before the first recursion should reach the end of the list and then count should be incremented in the recursion argument. the code presented is still not completely finished but please let me know if i am on the right track and also how i can possibly get this idea to work without any nullpointerexceptions or logical errors.
PLEASE explain this in words and NOT in code.
here is the code i have so far.
LINK CODE
public class link
{
public int dData;
public link next;
public link(int dd)
{
dData = dd;
}
public void displayLink()
{
System.out.print(dData);
}
}
LINKED LIST CODE
public class linkedList
{
private link first;
public linkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(int id)
{
link newLink = new link(id);
newLink.next = first;
first = newLink;
}
public void recursiveReverse(int count, int sizeOfList)
{
link current = first;
link previous = first;
for(int i = count; i < sizeOfList; i++)
{
if(current.next == null)
{
return;
}
else
{
previous = current;
current = current.next;
}
}
recursiveReverse(count++, sizeOfList);
if(current != first)
{
current.displayLink();
}
else
{
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps