麻烦打印反向链表(java)

时间:2013-10-06 03:47:57

标签: java

基本上,我创建了一些方法,用于打印整数链表,从链表中删除重复项,以及反转链表。一切正常......差不多。出于某种原因,我无法在列表被反转后让我的printLinked()方法工作。暂时,我只是创建了一个while循环来输出反转列表,这样我至少可以确保列表正在被反转。不过,我需要让printLinked()方法执行此操作。如果有人可以帮助我或指出我在找出问题所在的正确方向,那么我会非常感激。 谢谢。

    import java.util.Scanner;

公共类ListTest  {

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

    int input;

    Scanner keyboard = new Scanner (System.in);

    System.out.println("Enter integers into list; end by entering a zero");
    input = keyboard.nextInt();

    ListNode temp;

    while (input != 0)
    {

        temp = new ListNode();
        temp.data = input;
        temp.link = null;
        tail.link = temp;
        tail = temp;

        input = keyboard.nextInt();


    }

    System.out.println("You entered the following integers : ");
    printLinked(head.link);

    delrep(head);
    System.out.println("After deleting repeating integers, you are left with : ");
    printLinked(head.link);


    System.out.println("Your inverted list of integers is now : ");
    invert(head);

    printLinked(head.link);








}


    public static void printLinked(ListNode list)
    {
        ListNode cursor = list;

        while (cursor != null)
        {

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



        System.out.println("");


    }


        public static void delrep(ListNode head)
        {

            ListNode temp = new ListNode();




            while(head != null)
            {
                temp = head;

                while(temp.link != null)
                 {
                    if(head.data == temp.link.data)
                    {
                        ListNode temp2 = new ListNode();
                        temp2 = temp.link;
                        temp.link = temp2.link;
                        temp2 = null;
                    }
                    else
                    {
                         temp = temp.link;
                    }
                  }

                   head = head.link;
                }

            }


public static void invert(ListNode head)
        {

            ListNode temp1 = null;
            ListNode temp2 = null;


            while (head != null)
            {
                temp1 = head.link;
                head.link = temp2;
                temp2 = head;
                head = temp1;



            }

            head = temp2;





                         //This portion of code needs to be removed. I added this part just so I can visually
                         //confirm that the list is reversing until I can get the print method to work for the
                         // reversed list.

            while (head.link != null)
            {
                System.out.print(head.data + " ");
                head = head.link;
            }
            System.out.println("");


    }

}

另外,这就是我的ListNode类:

    public class ListNode

{

public int data;
public ListNode link;

}

1 个答案:

答案 0 :(得分:0)

你需要以你的反转方法返回列表的新头:

public static ListNode invert(ListNode head) {
    ListNode temp1;
    ListNode temp2 = null;

    while (head != null) {
        temp1 = head.link;
        head.link = temp2;
        temp2 = head;
        head = temp1;
    }

    return temp2;
}

打印时使用返回的值:

System.out.println("Your inverted list of integers is now : ");
ListNode result = invert(head);

printLinked(result.link);

它没有打印,因为你使用的是现在是列表尾部的原始头部。