回答
您需要定义一个节点数据结构,其中包含一些数据和对链表中下一个节点的引用。喜欢的东西:
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
}
然后,你可以写一个算法遍历以相反的顺序列表,构造一个新的反向列表。
reversed_list = new
for all node in the original list
insert the node to the head of reversed_list
如果我不想使用任何新的链表,那么算法将是什么。 – 2010-01-15 09:50:54
@Pritam,acutally,我还没有使用任何新的列表。 'reversed_list'只是一个指向新列表头的指针。换句话说,不需要记忆。 – pierrotlefou 2010-01-15 10:09:49
那效率不高。 – DarthVader 2011-06-07 22:43:33
使用循环(当前元素:currentNode,变量外循环initialzied:previousNode,nextNode)
Set nextNode = currentNode.NextNode
Set currentNode.NextNode = previousNode
Set previousNode = currentNode
Set currentNode = nextNode
continue with loop
由于这是有可能的功课,我要去的方式,可能是这个状态相当混乱,以免做所有的工作。希望我的尝试不会让事情变得更混乱(这很有可能)。
当你有一个对列表中的节点的引用(比如说第一个节点)时,还可以引用它后面的节点。您只需要让以下节点引用您的当前节点,同时保留有关下一个节点(及其以前的状态)的足够信息以执行类似的工作。现在唯一棘手的部分是处理边界条件(列表的开始和结束)。
这里是使用递归。
//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);
}
}
以下链接逆转迭代和递归在.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;
}
- 1. 使用递归来反转字符串
- 2. 使用递归反转序列
- 3. 反向单链表的递归方法?
- 4. 反向链表 - 递归
- 5. 反向链表递归
- 6. 如何使用递归来反转一个数字
- 7. 关于递归实现反转单向链表的问题
- 8. 使用递归的反向链接列表
- 9. Ç - 使用递归打印链表
- 10. 使用递归函数反转字符串列表
- 11. 在java中使用递归反转整数列表阵列
- 12. Scala:递归使用未来
- 13. 使用递归扭转链表产生错误的输出
- 14. 反转由递归
- 15. 通过递归来反转节点
- 16. 反向链接列表递归
- 17. 递归反向链接列表
- 18. 反向使用递归的字符串
- 19. 递归/链表
- 20. 链表递归...
- 21. 链表,递归
- 22. 使用递归求总和
- 23. 在类方法中递归地反转链表
- 24. 使用JUnit测试反转单个链接列表的方法
- 25. 使用递归
- 26. 使用递归
- 27. 使用递归
- 28. 使用递归
- 29. 使用递归
- 30. 使用递归在C中反转字符串
或者你可以使用内置的链接列表,但只使用链接在一个方向,你应该检查你的导师,如果这是可以接受的,虽然 – 2010-01-15 09:48:51
所以基本上你告诉我C#实际上有指针,他们宣布与一个“_”? – BlackBear 2011-05-12 12:30:36
编号C#使用与指针相似(但不相同)的引用。引用足以实现一个链表,你不需要指针。 顺便说一句,C#**没有**指针,但它们几乎不需要,不推荐用于正常使用 – 2011-05-12 13:11:46