如何把一个单链表倒序输出

怎么把一个单链表倒序输出就是12345变成54321不能重建一个链表或者堆栈什么的。。。求代码[最优解释]public v

怎么把一个单链表倒序输出
就是12345变成54321不能重建一个链表或者堆栈什么的。。。求代码
[最优解释]
public void turnList() throws Exception
{
if(head==null)
throw new Exception("null list");
else 
if(head==tail)
;
else
{
Node new_head=head;
while(head.next.next!=null)
{
Node temp=head.next;
head.next=temp.next;
temp.next=new_head;
new_head=temp;
}
tail.next=new_head;
Node temp=tail;
tail=head;
tail.next=null;
head=temp;
}
}
[其他解释]
这种东西很无聊诶

不知道写这个有什么用,给你写一个吧。Java的



package link;
/**
 * 节点对象不用我写注释了吧
 * @author 
 * 创建时间:2011-4-1 上午10:15:18
 * 类说明:
 */
public class Node {

private int value = -1;

private Node next = null;

public Node(int value){
this.value = value;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

@Override
public String toString() {
return "Node [value=" + value + "]";
}

}






package link;
/**
 * 链表类,注释我都加了
 * @author 
 * 创建时间:2011-4-1 上午10:16:46
 * 类说明:
 */
public class NodeList {

private Node head = null;

public Node getHead(){
return this.head;
}

/**
 * 对外公布的添加节点的方法
 * @param node
 */
public void addNode(Node node){
if(this.head == null){
/**
 * 如果头节点是空,就把要加的节点给头节点
 */
this.head = node;
}else{
/**
 * 否则把这个节点加到头节点后面的节点上
 */
this.addSubNode(this.head, node);
}
}

/**
 * 反向输出节点元素
 */
public void reversePrint(){
int size = this.getSize(0, this.head);

for(int index = size; index > 0; index--){
System.out.println(this.getNode(index));
}

}

/**
 * 给一个节点加子节点的方法
 * @param destNode
 * @param node
 */
private void addSubNode(Node destNode, Node node){
if(destNode.getNext() == null){
/**
 * 如果这个节点没有子节点则把新节点设置为这个节点的子节点
 */
destNode.setNext(node);
}else{
/**
 * 否则把新节点加到这个节点的子节点的后面
 */
this.addSubNode(destNode.getNext(), node);
}
}

/**
 * 得到下标对应的节点对象
 * @param index
 * @return
 */
private Node getNode(int index){

int start = 1;
Node currentNode = this.head;
while(start < index){


currentNode = currentNode.getNext();
start++;
}

return currentNode;
}
/**
 * 计算链表里节点个数的方法
 * @param size
 * @param currentNode
 * @return
 */
private int getSize(int size, Node currentNode){
if(currentNode.getNext() == null){
return ++size;
}else{
return getSize(++size, currentNode.getNext());
}
}
}




package link;
/**
 * 测试类,自己看吧。
 * @author 
 * 创建时间:2011-4-1 上午10:15:59
 * 类说明:
 */
public class NodeListConstructor {


public static void main(String[] args){
NodeList list = new NodeList();
list.addNode(new Node(3));
list.addNode(new Node(1));
list.addNode(new Node(2));
list.addNode(new Node(4));
list.addNode(new Node(5));
list.addNode(new Node(6));
list.reversePrint();
}
}




不借助其他存储区域的话,你做什么程序都做不了的。

最少你做for循环要申请循环变量吧?

[其他解释]
已有一个单链表
只用申请3个节点的空间就可以在O(n)内进行单链表的逆转
[其他解释]
不建别的东西就只能一个一个输出来啦

用的什么链表
[其他解释]
用的是单链表的话
先把该单链表逆转下
[其他解释]
单向链表的话,不借助其他的链表或其他的存储区只能是单向的输出,因为只有知道上一个节点才能找到先一个节点。如果双向链表直接可以逆序输出。
[其他解释]
就是2#的那种方法了,先逆转单链表,然后在输出就可以了
[其他解释]
你是说不能用堆栈原理?
[其他解释]

Node *Reverse(Node *&head)
{
Node *p1,*p2,*p3;
p1=head;
if (NULL==head