昏暗试图用 jgrasp 写一条倒车线并继续收到错误,有人看到我做错了什么吗?

问题描述

我的任务涉及使用递归方法。编写一个程序来反转 LinkedList。这是我在下面做的代码,谁能看到我做错了什么,非常感谢!

PS:这是在 jgrasp 中完成的

//用于反转链表的Java程序

class MyLinkedList {

static Node head;


static class Node {

    int data;

    Node next;



    Node(int d) {

        data = d;

        next = null;

    }

}



/* Function to reverse the linked list */

Node reverse(Node node) {
    Node prev = null;
    Node current = node;
    Node next = null;
    while (current != null) {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    node = prev;
    return node;
}

// 打印双链表的内容

void printList(Node node) {

    while (node != null) {

        System.out.print(node.data + " ");

        node = node.next;

    }

}



public static void main(String[] args) {

    MyLinkedList list = new MyLinkedList();
        list.head = new Node(85);
        list.head.next = new Node(15);
        list.head.next.next = new Node(4);
        list.head.next.next.next = new Node(20);

        System.out.println("Given Linked list");
        list.printList(head);
        head = list.reverse(head);
        System.out.println("");
        System.out.println("Reversed linked list ");
        list.printList(head);

     }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)