【发布时间】:2019-03-23 00:50:26
【问题描述】:
我无法解决此代码问题。我需要使用 Java 中的递归方法来反转链表。我似乎无法弄清楚,我已经尝试了我的知识范围。非常感谢您的帮助。
// Java program for reversing the linked list
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) {
add content
node = prev;
return node;
}
// prints content of double linked list
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();
add content }
}
【问题讨论】:
-
为什么需要递归执行此操作?列表是双向链表吗?这是作业吗?!
-
您到底尝试过什么?这个问题听起来像家庭作业或实验室。针对这个特定问题,Google 上有大量资源。