简体   繁体   English

反向打印链表(单独和双重)的最佳方法?

[英]Best way to print a linked list(Singly and Doubly) in reverse?

Can someone provide the possible ways to print a Linkedlist in reverse in Java.有人可以提供在 Java 中反向打印 Linkedlist 的可能方法吗? A way I understand would be to Recursively reach the end of list, And then start printing from the back and come to front recursively.我理解的一种方法是递归到达列表的末尾,然后从后面开始打印并递归地到达前面。 Please share the possible ways.请分享可能的方法。

I am using a Node having next and previous.我正在使用具有下一个和上一个的节点。

A Solution I figured is below.我想出的解决方案如下。 But here I need to create a variable each time entering in the recursive loop.但是在这里每次进入递归循环时我都需要创建一个变量。 That's bad :(那很糟 :(

public void reversePrinting(int count){
        if(count==0){       //to assign the root node to current only once
            current=root;
            count++;
        }
        else{               //moving current node to subsequent nodes
        current=current.nextNode;
        }
        int x= current.data;
        if(current.nextNode==null){
            System.out.println(x);
            return;
        }
        reversePrinting(count);
            System.out.println(x);
    }

try this, it is able reverse a linkedlist试试这个,它可以反转链表

public class DoReverse{ 
    private Node head; 
    private static class Node {
        private int value; 
        private Node next; 
        Node(int value) { 
            this.value = value; 
            } 
        } 
    public void addToTheLast(Node node) { 
        if (head == null) {
            head = node; 
        } 
        else { 
            Node temp = head;
            while (temp.next != null) 
                temp = temp.next; 
            temp.next = node; 
            } 
        } 
    public void printList(Node head) { 
        Node temp = head; 
        while (temp != null) { 
            System.out.format("%d ", temp.value); 
            temp = temp.next; 
            } 
        System.out.println(); 
        } 

    public static Node reverseList(Node head){

        Node prev = null;
        Node current = head;
        Node next = null;

        while(current != null){
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        head = prev;
        return head;

    }

    public static void main(String[] args) { 
        DoReverse list = new DoReverse(); 
        // Creating a linked list 
        Node head = new Node(5);
        list.addToTheLast(head); 
        list.addToTheLast(new Node(6)); 
        list.addToTheLast(new Node(7)); 
        list.addToTheLast(new Node(1)); 
        list.addToTheLast(new Node(2)); 
        list.addToTheLast(new Node(10));
        System.out.println("Before Reversing :");
        list.printList(head); 


        Node reverseHead= list.reverseList(head);
        System.out.println("After Reversing :");
        list.printList(reverseHead);


        } 
}

Why not copy the list so that it is reversed:为什么不复制列表以使其反转:

Reversing a linked list in Java, recursively 递归地反转Java中的链表

And then loop the copy of the list like your normally would?然后像往常一样循环列表的副本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM