简体   繁体   English

如何反转链表?

[英]How to reverse a Linked List?

I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints.我正在为我的数据结构类开发一个项目,该项目要求我编写一个类来实现一个整数链表。

  • Use an inner class for the Node.为节点使用内部类。
  • Include the methods below.包括以下方法。
  • Write a tester to enable you to test all of the methods with whatever data you want in any order.编写一个测试器,使您能够以任何顺序使用您想要的任何数据测试所有方法。

I have to create a method called "public LinkedListOfInts reverse()".我必须创建一个名为“public LinkedListOfInts reverse()”的方法。 This method is meant to "Return a copy of your Linked List but in reverse order."此方法旨在“以相反的顺序返回链接列表的副本”。 I have my code for this method down below.我在下面有这个方法的代码。 However, when I try to reverse a list it only prints the head.但是,当我尝试反转列表时,它只会打印头部。 For Example, if I have a list like "[16, 1, 8, 7, 10, 10, 14, 17, 11, 4,] and I try to reverse it my output is [ 16, ]. Does someone know how correct my code so I can reverse a linked list?例如,如果我有一个像 "[16, 1, 8, 7, 10, 10, 14, 17, 11, 4,] 这样的列表,我尝试将其反转,我的输出是 [ 16, ] 。有人知道怎么做吗?更正我的代码以便我可以反转链接列表?

import java.util.Random;
import java.util.Scanner;

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

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

    }

    public LinkedListOfInts(LinkedListOfInts other) {
        Node tail = null;
        for (Node n = other.head; n != null; n = n.nextNode) {
            if (tail == null)
                this.head = tail = new Node(n.value, null);
            else {
                tail.nextNode = new Node(n.value, null);
                tail = tail.nextNode;
            }
        }
    }

    public LinkedListOfInts(int[] other) {
        Node[] nodes = new Node[other.length];
        for (int index = 0; index < other.length; index++) {
            nodes[index] = new Node(other[index], null);
            if (index > 0) {
                nodes[index - 1].nextNode = nodes[index];
            }
        }

        head = nodes[0];
    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i++)
            this.addToFront(random.nextInt(high - low) + low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public LinkedListOfInts reverse() {
        if (head == null)
            return null;
        Node current = head;
        Node previous = null;
        Node nextNode = null;
        while (current != null) {
            nextNode = current.nextNode;
            current.nextNode = previous;
            previous = current;
            current = nextNode;
        }
        return this;
    }

    public String toString() {
        String result = " ";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
            result += ptr.value + " ";
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        LinkedListOfInts copy = new LinkedListOfInts(list);
        boolean done = false;
        while (!done) {
            System.out.println("1. Reverse");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 11:
                System.out.println("Reverse the List");
                System.out.println(copy.reverse());
                break;
            case 12:
                System.out.println("toString");
                System.out.println(list.toString());
                break;
            }
        }
    }
}

Here is a working example for reversing your LinkedList.这是一个用于反转 LinkedList 的工作示例。 Keep in mind that you are not copying the content of the LinkedList.请记住,您不是在复制 LinkedList 的内容。 So if you reverse the list, the original list's head is now the tail and returns only one int.因此,如果您反转列表,则原始列表的头部现在是尾部并且仅返回一个 int。

import java.util.Random;
import java.util.Scanner;

public class LinkedListOfInts{
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

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

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", nextNode=" + nextNode +
                    '}';
        }
    }

    public LinkedListOfInts(LinkedListOfInts other) {
        System.out.println(other.tail);
        head = other.head;
        tail = other.tail;

        System.out.println(this);

    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i++) {
            this.addToFront(random.nextInt(high - low) + low);
        }
        Node node=head;
        while(node.nextNode!=null){
            node = node.nextNode;
        }
        tail = node;
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public LinkedListOfInts reverse() {
        Node previous = null;
        Node curr = head;
        Node nex;
        while (curr != null)
        {
            nex = curr.nextNode;
            curr.nextNode = previous;
            previous = curr;
            curr = nex;
        }
        head = previous;
        return this;
    }

    public String toString() {
        StringBuilder result = new StringBuilder(" ");
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
            result.append(ptr.value).append(" ");
        return result.toString();
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        LinkedListOfInts copy = new LinkedListOfInts(list);
        boolean done = false;
        while (!done) {
            System.out.println("1. Reverse");
            System.out.println("2. toString");
            switch (input.nextInt()) {
                case 1:
                    System.out.println("Reverse the List");
                    System.out.println(copy.reverse());
                    break;
                case 2:
                    System.out.println("toString");
                    System.out.println(list);
                    break;
            }
        }
    }
}

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

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