如何反转链表?

How to reverse a Linked List?

我正在为我的数据结构 class 开发一个项目,该项目要求我编写一个 class 来实现一个整数链表。

  • 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.

我必须创建一个名为“public LinkedListOfInts reverse()”的方法。此方法旨在“Return 链接列表的副本,但顺序相反。”我在下面有这个方法的代码。但是,当我尝试反转列表时,它只会打印头部。例如,如果我有一个像“[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;
            }
        }
    }
}

这是一个反转 LinkedList 的工作示例。请记住,您并不是在复制 LinkedList 的内容。所以如果你反转列表,原来列表的头部现在是尾部并且 returns 只有一个 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;
            }
        }
    }
}