2010-01-15 65 views

回答

0

您需要定义一个节点数据结构,其中包含一些数据和对链表中下一个节点的引用。喜欢的东西:

class Node { 
    private Node _next; 
    private string _data; 

    public Node(string data) { 
    _next = null; 
    _data = data; 
    } 

    // TODO: Property accessors and functions to link up the list 
} 

然后,你可以写一个算法遍历以相反的顺序列表,构造一个新的反向列表。

+0

或者你可以使用内置的链接列表,但只使用链接在一个方向,你应该检查你的导师,如果这是可以接受的,虽然 – 2010-01-15 09:48:51

+0

所以基本上你告诉我C#实际上有指针,他们宣布与一个“_”? – BlackBear 2011-05-12 12:30:36

+0

编号C#使用与指针相似(但不相同)的引用。引用足以实现一个链表,你不需要指针。 顺便说一句,C#**没有**指针,但它们几乎不需要,不推荐用于正常使用 – 2011-05-12 13:11:46

0
reversed_list = new 
for all node in the original list 
    insert the node to the head of reversed_list 
+0

如果我不想使用任何新的链表,那么算法将是什么。 – 2010-01-15 09:50:54

+0

@Pritam,acutally,我还没有使用任何新的列表。 'reversed_list'只是一个指向新列表头的指针。换句话说,不需要记忆。 – pierrotlefou 2010-01-15 10:09:49

+0

那效率不高。 – DarthVader 2011-06-07 22:43:33

1

使用循环(当前元素:currentNode,变量外循环initialzied:previousNode,nextNode)

Set nextNode = currentNode.NextNode 
Set currentNode.NextNode = previousNode 
Set previousNode = currentNode 
Set currentNode = nextNode 
continue with loop 
0

由于这是有可能的功课,我要去的方式,可能是这个状态相当混乱,以免做所有的工作。希望我的尝试不会让事情变得更混乱(这很有可能)。

当你有一个对列表中的节点的引用(比如说第一个节点)时,还可以引用它后面的节点。您只需要让以下节点引用您的当前节点,同时保留有关下一个节点(及其以前的状态)的足够信息以执行类似的工作。现在唯一棘手的部分是处理边界条件(列表的开始和结束)。

2

这里是使用递归。

​​
0
//Have tried the Iterative approach as below, feel free to comment/optimize 

package com.test; 


public class ReverseSinglyLinkedList { 


public ReverseSinglyLinkedList() { 
    // TODO Auto-generated constructor stub 
} 
public Node ReverseList(Node n) 
{ 

    Node head = n; 
    Node current = n; 
    Node firstNodeBeforeReverse = n; // keep track of origional FirstNode 

    while(true) 
    { 

     Node temp = current; 
     // keep track of currentHead in LinkedList "n", for continued access to unprocessed List 
     current = current.next; 
     temp.next = head; 
      // keep track of head of Reversed List that we will return post the processing is over 
     head = temp; 

     if(current.next == null) 
     { 

      temp = current; 
      current.next = head; 
      head = temp;   
          // Set the original FirstNode to NULL 
      firstNodeBeforeReverse.next = null; 

      break; 
     } 
    } 

    return head; 

} 

public void printLinkList(Node n) 
{ 

    while(true) 
    { 
     System.out.print(n.data + " "); 
     n = n.next; 
     if(n.next ==null) 
     { 
      System.out.print(n.data + " "); 
      break; 
     } 

    } 
} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    // TEST THE PROGRAM: crate a node List first to reverse it 
    Node n = new Node(1); 
    n.next = new Node(2); 
    n.next.next = new Node(3); 
    n.next.next.next = new Node(4); 
    n.next.next.next.next = new Node(5); 
    n.next.next.next.next.next = new Node(6); 

    ReverseSinglyLinkedList r = new ReverseSinglyLinkedList(); 
    System.out.println("Input Linked List : "); 
    r.printLinkList(n); 

    Node rsList = r.ReverseList(n); 

    System.out.println("\n Reversed Linked List : "); 
    r.printLinkList(rsList); 



} 

} 
0

以下链接逆转迭代和递归在.net中(C#) (注意链表保持第一和最后一个三分球,这样我可以在O附加在年底或插入在头(1) - 一个没有做到这一点,我只是定义我上面的链接列表行为)

public void ReverseIterative() 
     { 
      if(null == first) 
      { 
       return; 
      } 
      if(null == first.Next) 
      { 
       return; 
      } 
      LinkedListNode<T> p = null, f = first, n = null; 
      while(f != null) 
      { 
       n = f.Next; 
       f.Next = p; 
       p = f; 
       f = n; 
      } 
      last = first; 
      first = p; 
     } 

递归:

 public void ReverseRecursive() 
     { 
      if (null == first) 
      { 
       return; 
      } 
      if (null == first.Next) 
      { 
       return; 
      } 
      last = first; 
      first = this.ReverseRecursive(first); 
     } 
     private LinkedListNode<T> ReverseRecursive(LinkedListNode<T> node) 
     { 
      Debug.Assert(node != null); 
      var adjNode = node.Next; 
      if (adjNode == null) 
      { 
       return node; 
      } 
      var rf = this.ReverseRecursive(adjNode); 
      adjNode.Next = node; 
      node.Next = null; 
      return rf; 
     }