【问题标题】:How to print a reverse linked list without using recursion in Java?如何在不使用 Java 递归的情况下打印反向链表?
【发布时间】:2016-10-08 23:25:13
【问题描述】:

我尝试在不递归的情况下打印反向链表并反转链表。我该怎么做?

问题:如何在不使用递归且不反转列表的情况下打印反向链表?

要求:没有多余的空格,不能反转链表,不能使用递归。

这里是链表Node的定义

class Node {
  int value;
  Node next;

  public Node(int val) {
    this.value = val;
  }
}

这是我的 printReverseLinkedList 的递归版本:

public void printReverseList(Node head) {
    Node temp = head;
    if (temp.next != null) {
        printReverseList(temp.next);
    }
    System.out.print(temp.value);
}

性能无所谓,因为我只想这样做。

【问题讨论】:

  • 构建一个反向字符串?
  • 不,这不是正确的方法。 @JornVernee
  • 我认为应该是 temp != null,即只要头部不为空 While(temp != null)
  • 性能重要吗?你可以在O(n^2)中做到这一点,第一个指针是当前迭代的索引,第二个指针是记住之前迭代打印的索引元素。
  • 在java中没有指针之类的东西

标签: java recursion linked-list iteration singly-linked-list


【解决方案1】:

如果您既不能反转列表,也不能使用递归,那么这样做的唯一方法是:

public void printReversList(Node head) {

    Node current = head; // used to search through the list
    Node last    = null; // stores the last element that we printed

    while (last != head) { // = we didn't print everything yet

        // find next element to print - it's one element before we reach "last"
        while (current.next != last) {
            current = current.next;
        }

        // Store the current element as the new last and print it
        last  = current;
        system.out.print(last.value);

        // reset current and start all over
        current = head;
    }
}

这是非常无效的,但我想不出其他方法。

【讨论】:

  • 我将您的代码复制到 eclipse 中并将 Node last = current 更改为 last = current 并且我在 system.out.print(last.value) 中得到了一个 nullpointerexception? @Johannes H.
  • 你能设置一个条件再试一次if (head == null) return; 。将方法定义放在开头。 Node last = current to last = current 应该是编写代码的正确方式。
  • @SHE Eclipse 是正确的。那是我的一个复制和粘贴错误 - 我修复了它。但是我不太明白NullPointerException - 我无法测试我的代码,但它不应该达到last == null 的状态,除非head == null
【解决方案2】:

使用堆栈然后弹出怎么样?你说使用另一个数据结构就可以了。这不是很好的代码,但是应该可以完成工作。

public void printReversList(Node head) {

    Stack<Node> stack = new Stack<>();

    while (head != null){

       stack.push(head);
       head = head.next;          
   }

   while (!stack.isEmpty()){
       System.out.println(stack.pop());
   }
}

【讨论】:

  • 堆叠额外空间? @Arefe
  • 好的,那很抱歉,但是,我之前问过你。
【解决方案3】:

你可以试试这个:

public void printReverseList(Node head) {
            if(head == null) return;
            Node prev = null;
            Node revers = head;
            Node nex = null;

            while (revers != null){
                nex = revers.next;
                revers.next = prev;

                prev = revers;
                revers = nex;

            }
            System.out.println(prev);
        }

【讨论】:

  • 我需要打印,而不是反向。 @Seek Addo
  • 是的,您可以在节点中调用反向,然后打印它。 @SHE
  • 我认为不允许更改Node 的定义
  • 似乎太复杂了@SeekAddo
  • 节点的定义不同@SeekAddo
【解决方案4】:
void ReversePrint(Node head) {
    // This is a "method-only" submission.
    // You only need to complete this method. 
    Stack<Node> stk=new Stack<>();

    Node temp=head;
    while(temp!=null){
        stk.push(temp);
        temp=temp.next;
    }
    while(!stk.isEmpty()){
        System.out.println(stk.pop().data);
    }
}

【讨论】:

    【解决方案5】:
    public void printReverseList(Node head) {
            Node node = head;
            List<Integer> list = new ArrayList<Integer>();
                if (head == null){
                    System.out.println(head.data); 
                }
                else{
                   while (node != null){
                    list.add(0, node.data);
                    node = node.next;
                }
                for (int item:list){
                    System.out.println(item);
                }
            }
        }
    

    【讨论】:

    • 不鼓励仅使用代码的答案。请提供一些附带的解释
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2013-06-02
    • 2022-12-05
    • 1970-01-01
    • 2014-03-01
    • 2020-01-22
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多