0

The problem is that in the inverted method the program never shows the first number. For example if the numbers to be reversed is 1 2 3 4, the out put is 3 2 1 0. there is no 4 and the 0 should not be there. please help.

import java.util.Scanner;


public class LinkTest
{
    public static void main(String [] args)
    {
        ListNode head = new ListNode();
        ListNode tail = head;
        int x;
        head.link = null;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a list of integers ending in zero.");

        x = keyboard.nextInt();

        while(x != 0)
        {

            ListNode newOne = new ListNode();
            newOne.data = x;
            newOne.link = null;
            tail.link = newOne;
            tail = newOne;
            x = keyboard.nextInt();
        }

            printLinked(head);

            delRep(head);

            invert(head);

    }

    public static void printLinked(ListNode cursor)
    {

        while(cursor.link != null)
    {

        System.out.print(cursor.link.data + " ");
        cursor = cursor.link;
    }

    System.out.println();
    }

    public static void delRep(ListNode num)
    {   

        ListNode current = num.link;
        ListNode cursor = null;
        ListNode duplicate = null;

    while(current != null && current.link != null)
    {
        cursor = current;

        while(cursor.link != null)
        {
            if(current.data == cursor.link.data)
            {
                duplicate = cursor.link;
                cursor.link = cursor.link.link;
            }
            else
            {
                cursor = cursor.link;
            }
        }

        current = current.link;
    }


    System.out.println("Here is the list without repeated ");
    printLinked(num);


    }

public static void invert(ListNode head)
{
    ListNode previous = null;
    ListNode current = head;
    ListNode forward;

    while (current != null) 
    {
        forward = current.link;
        current.link = previous;
        previous = current;
        current = forward;
    }

        System.out.println("Here is the inverted list.");
        printLinked(previous);

    }
}
  • Have you stepped through the code in your IDE debugger? If not, start there. 99% of the time this will help you find the problem very quickly. If you do not know how to do that LEARN ASAP by using Google to find information on using your debugger. Debugging in an IDE is a fundamental skill you MUST acquire now. – Jim Garrison Feb 28 '16 at 17:21
  • Do the program on paper first. – Joop Eggen Feb 28 '16 at 17:26

1 Answers1

0

Your issue is that the head of your linked list is a dummy. For input 1 2 3 4 you create this:

0->1->2->3->4

When printing, you skip the first node.

But the invert method doesn't treat the head as a dummy. It reverts the list to this:

4->3->2->1->0

And then, since the printing method skips the head, the output you'll get is 3 2 1 0.

Here's the fix:

public static void invert(ListNode head)
{
    ListNode previous = null;
    ListNode current = head.link;
    ListNode forward;

    while (current != null)
    {
        forward = current.link;
        current.link = previous;
        previous = current;
        current = forward;
    }

    System.out.println("Here is the inverted list.");
    head.link = previous;
    printLinked(head);
}
janos
  • 120,954
  • 29
  • 226
  • 236
  • Thank you so much. i have been stuck on this for awhile. i didn't think to look in the print method because my delRep worked fine so i assumed that wasn't the issue. Also thank you for explaining it as well rather than just giving me the solution. This helped a lot. – rumbledamage Feb 28 '16 at 18:22