1

I have written this program that reverses a singly linked list and prints it. The problem is that I can't print the reverse order and it gives me StackOverFlowError. The problem may be with my Reverse method. Any help will be appreciated.

private static Node head;

public static void main(String[] args) 
{
    GettingNumbers();
}

public static void Reverse(Node node)
{
    if (node != null)
    {
        Reverse(node.next);
        System.out.print(" " + node.key);
    }
}
public static void GettingNumbers()
{   
    head = new Node();
    head = null;
    Node last = new Node();
    String str = new String();

    System.out.print("Enter a number or # to stop: ");
    str = console.next();

    while (!(str.trim().equals("#")))
    {
        Node node = new Node();
        node.key = str;
        node.next= null;

        if (head == null)
        {
            head = node;
            last = node;
        }
        else
        {
            last.next = node;
            last = node;
        }

        System.out.print("Enter a number or # to stop: ");
        str = console.next();
    }
    Node h = head;
    if (head == null || head.next == null)
    {
        return;  //empty or just one node in list
    }

    Node Second = head.next;

    //store third node before we change 
    Node Third = Second.next;  

    //Second's next pointer
    Second.next = head;  //second now points to head
    head.next = null;  //change head pointer to NULL

    //only two nodes, which we already reversed
    if (Third == null)
    {
        return;  
    }

    Node CurrentNode = Third;

    Node PreviousNode = Second;

    while (CurrentNode != null)
    { 
        Node NextNode = CurrentNode.next;

        CurrentNode.next = PreviousNode;

        /*  repeat the process, but have to reset
             the PreviousNode and CurrentNode
        */

        PreviousNode = CurrentNode;
        CurrentNode = NextNode;  

    }

    head  = PreviousNode; //reset the head node
    Reverse(h);

    return;

}
}
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
Parisa
  • 159
  • 1
  • 2
  • 8
  • 1
    The problem with recursion for tranversing a list is that you end up with a method call on the memory stack for each element in the list. Once the list is beyond a trivial size, you run the risk of using all your memory and getting a `StackOverflowError` – John B Nov 03 '14 at 14:47
  • You should remove all the `SecondNode`, `ThirdNode` stuff to try to localize the issue. You have so much odd stuff going on there it is hard to determine if you have created an look in your list. – John B Nov 03 '14 at 14:51
  • what these two lines says `head = new Node(); head = null;` – Braj Nov 03 '14 at 14:52
  • 1
    Comment update... it is hard to determine if you have created a loop in your list. This is usually the cause of this error: a loop or a recursion that is too deep. – John B Nov 03 '14 at 14:59

1 Answers1

0

change your code as below:

 Node previousNode=null;
      Node nextNode;
      while(currentNode!=null)
      {
       nextNode=currentNode.next;
      // reversing the link
       currentNode.next=previousNode;
      // moving currentNode and previousNode by 1 node
       previousNode=currentNode;
       currentNode=nextNode;
      }

Have a look at this answer with detailed explanation: https://stackoverflow.com/a/44068199/3148734

Community
  • 1
  • 1
bpjoshi
  • 1,091
  • 16
  • 23