4

我试图使用递归来反转链表。我得到了解决方案,但无法解决在互联网上找到的以下问题。

使用递归反转链表,但函数应具有 void 返回类型。

我能够实现返回类型为Node的函数。下面是我的解决方案。

public static Node recursive(Node start) {

    // exit condition
    if(start == null || start.next == null)
        return start;

    Node remainingNode = recursive(start.next);

    Node current = remainingNode;

    while(current.next != null)
           current = current.next;

    current.next = start;
    start.next = null;
    return remainingNode;
 }

我无法想象这个问题是否会有这样的解决方案。

有什么建议么 ?

4

10 回答 10

3

经过测试,它可以工作(假设您有自己的链表实现,其中Nodes 知道该next节点)。

public static void reverse(Node previous, Node current) {
    //if there is next node...
    if (current.next != null) {
        //...go forth and pwn
        reverse(current, current.next);
    }

    if (previous == null) {
        // this was the start node
        current.next= null;
    } else {
        //reverse
        current.next= previous;
    }   
}

你用它来称呼它

reverse(null, startNode);
于 2013-07-08T11:51:10.323 回答
1
public void recursiveDisplay(Link current){

        if(current== null)
           return ;
        recursiveDisplay(current.next);
        current.display();
}
于 2014-02-12T11:16:51.553 回答
1
static StringBuilder reverseStr = new StringBuilder();

public static void main(String args[]) {
    String str = "9876543210";
    reverse(str, str.length() - 1);
}

public static void reverse(String str, int index) {
    if (index < 0) {
        System.out.println(reverseStr.toString());
    } else {
        reverseStr.append(str.charAt(index));
        reverse(str, index - 1);
        index--;
    }
}
于 2015-12-12T04:07:54.373 回答
0

这应该工作

static void reverse(List list, int p) {
    if (p == list.size() / 2) {
        return;
    }
    Object o1 = list.get(p);
    Object o2 = list.get(list.size() - p - 1);
    list.set(p, o2);
    list.set(list.size() - p - 1, o1);
    reverse(list, p + 1);
}

尽管要使用 LinkedList 高效,但应该将其重构为使用 ListIterator

于 2013-07-08T11:46:05.837 回答
0

我不熟悉Java,但这里有一个C++ 版本。反转列表后,仍然保留列表的头部,这意味着仍然可以从旧的列表头部访问列表List* h

void reverse(List* h) {
    if (!h || !h->next) {
      return;
    }
    if (!h->next->next) {
      swap(h->value, h->next->value);
      return;
    }
    auto next_of_next = h->next->next;
    auto new_head = h->next;
    reverse(h->next);
    swap(h->value, new_head->value);
    next_of_next->next = new_head;
    h->next = new_head->next;
    new_head->next = nullptr;
  }
于 2013-10-05T01:52:49.783 回答
0

试试这个代码 - 它确实有效

    public static ListElement reverseListConstantStorage(ListElement head) {
    return reverse(null,head);
}

private static ListElement reverse(ListElement previous, ListElement current) {

    ListElement newHead = null;
    if (current.getNext() != null) {
        newHead = reverse(current, current.getNext());
    } else {//end of the list
        newHead=current;
        newHead.setNext(previous);
    }

    current.setNext(previous);        
    return newHead;        
}
于 2013-10-12T20:46:48.493 回答
0
public static Node recurse2(Node node){
    Node head =null;
    if(node.next == null) return node;
    Node previous=node, current = node.next;
    head = recurse2(node.next);
    current.next = previous;
    previous.next = null;
    return head;

}

调用函数时,分配返回值如下:

 list.head=recurse2(list.head);
于 2014-01-06T07:59:25.570 回答
0

下面的函数基于从 darijan 选择的答案,我所做的只是添加 2 行代码,以便它适合你们想要工作的代码:

public void reverse(Node previous, Node current) {
        //if there is next node...
        if (current.next != null) {
            //...go forth and pwn
            reverse(current, current.next);
        }
        else this.head = current;/*end of the list <-- This line alone would be the fix
        since you will now have the former tail of the Linked List set as the new head*/


        if (previous == null) {
            // this was the start node
            current.next= null;
            this.tail = current; /*No need for that one if you're not using a Node in 
            your class to represent the last Node in the given list*/
        } else {
            //reverse
            current.next= previous;
        }   
    }

另外,我已将其更改为非静态函数,因此使用它的方法是:myLinkedList.reverse(null, myLinkedList.head);

于 2014-04-08T20:49:18.083 回答
0

这是我的版本 - void ReverseWithRecursion(Node currentNode) - 它是 LinkListDemo 类的方法,因此可以访问 head

  1. 基本情况 - 如果 Node 为空,则不执行任何操作并返回。如果 Node->Next 为 null,则“使其成为头部”并返回。
  2. 其他情况 - 反转 currentNode 的 Next。

    public void ReverseWithRecursion(Node currentNode){
       if(currentNode == null) return;
       if(currentNode.next == null) {head = currentNode; return;}
    
       Node first = currentNode;
       Node rest = currentNode.next;
    
       RevereseWithRecursion(rest);
    
       first.next.next = first;
       first.next = null;
    }
    

你这样称呼它——

LinkListDemo ll = new LinkListDemo(); // assueme class is available
ll.insert(1);  // Assume method is available
ll.insert(2);
ll.insert(3);
ll.ReverseWithRecursion(ll.head);
于 2015-01-14T15:53:08.433 回答
-1

我能想到的最简单的方法是:

public static <T> void reverse( LinkedList<T> list )
{
    if (list.size() <= 1) {
        return;
    }
    T first = list.removeFirst();
    reverse( list);
    list.addLast( first );
}
于 2013-07-08T11:57:04.783 回答