如何修复以下代码,以便获得输出的反向链接列表?

问题描述 投票:0回答:2

以下代码是我尝试实现SinglyLinkedListNodeSinglyLinkedList类并使用SinglyLinkedListNode reverse(SinglyLinkedListNode head)方法来反转此链接列表的尝试。

输入由第一行详细说明测试用例的数量t组成。对于每个测试用例,第一行n代表链表中元素的数量。接下来的n行数将每个包含此列表中的一个元素,使得输入如下:

1 (number of test cases)
5 (number of elements in list)
1 (element in list)
2 (element in list)
3 (element in list)
4 (element in list)
5 (element in list)

如何修复以下代码,使其可以打印此反向链接列表,使输出如下:

5 4 3 2 1

因为我的代码改为打印出以下内容:

1
5
1
2
3
4
5

1 2 3 4 5 

我的代码:

import java.util.Scanner;

public class ReverseLinkedList {

    static class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;

        public SinglyLinkedListNode(int nodeData) {
            data = nodeData;
            next = null;
        }
    }

    static class SinglyLinkedList {
        private SinglyLinkedListNode head;
        private SinglyLinkedListNode tail;

        public SinglyLinkedList() {
            SinglyLinkedListNode head = null;
            SinglyLinkedListNode tail = null;
        }

        public void insertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);

            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }

            this.tail = node;
        }

        public SinglyLinkedListNode reverse(SinglyLinkedListNode head) {
            SinglyLinkedListNode previous = null;
            SinglyLinkedListNode current = head;
            SinglyLinkedListNode next = null;
            while (current != null) {
                next = current.next;
                current.next = previous;
                previous = current;
                current = next;
            }
            return previous;
        }

        public void printLinkedList() {
            SinglyLinkedListNode node = head;
            while (node != null) {
                System.out.print(node.data + " ");
                node = node.next;
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        SinglyLinkedList list = new SinglyLinkedList();

        int testCases = input.nextInt();

        if (testCases <= 10) {
            input.nextLine();
            int size = input.nextInt();
            if (size <= 1000) {
                for (int i = 0; i < size; i++) {
                    list.insertNode(input.nextInt());
                }
                list.reverse(list.tail);
                list.printLinkedList();
            }
        }
    }
}
java data-structures linked-list nodes reverse
2个回答
0
投票

0
投票
SinglyLinkedListNode previous = null; SinglyLinkedListNode current = head; SinglyLinkedListNode next = null; while (current != null) { next = current.next; current.next = previous; previous = current; current = next; } head= previous; return head;
也在您的方法中的printLinkedList设置

SinglyLinkedListNode node = tail; // instead of head

© www.soinside.com 2019 - 2024. All rights reserved.