1

基本上,我创建了打印整数链表、从链表中删除重复项以及反转链表的方法。一切正常......几乎。由于某种原因,我无法在列表反转后让我的 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;

}

4

1 回答 1

0

您需要在您的 invert 方法中返回列表的新头部:

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);

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

于 2013-10-06T04:20:05.430 回答