【问题标题】:How to do a reversed linked list [duplicate]如何做一个反向链表[重复]
【发布时间】:2016-06-11 16:11:18
【问题描述】:

问题在于,在倒转方法中,程序从不显示第一个数字。例如,如果要反转的数字是 1 2 3 4,则输出为 3 2 1 0. 没有 4 并且 0 不应该在那里。请帮忙。

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

    }
}

【问题讨论】:

  • 您是否在 IDE 调试器中单步执行了代码?如果没有,从那里开始。 99% 的时间,这将帮助您快速找到问题。如果您不知道如何做到这一点,请使用 Google 尽快了解有关使用调试器的信息。在 IDE 中进行调试是您现在必须掌握的一项基本技能。
  • 先把程序写在纸上。

标签: java linked-list


【解决方案1】:

您的问题是您的链接列表的head 是一个虚拟对象。对于输入 1 2 3 4,您可以创建:

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

打印时跳过第一个节点。

invert 方法不会将头部视为假人。它将列表还原为:

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

然后,由于打印方法跳过了头部,你将得到的输出是3 2 1 0

解决方法如下:

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

【讨论】:

  • 非常感谢。我已经坚持了一段时间。我没有考虑查看打印方法,因为我的 delRep 工作正常,所以我认为这不是问题。还要感谢您解释它,而不仅仅是给我解决方案。这帮助很大。
猜你喜欢
  • 2012-03-27
  • 2018-03-22
  • 2014-05-01
  • 2018-05-23
  • 1970-01-01
  • 2017-03-10
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多