千辛万苦啊!!
public class NodeDemo {
public static void main(String[] args)
{
Node head = new Node(0);
Node temp = null;
Node cur = null;
for(int i=1;i<10;i++)
{
temp = new Node(i);
if(i==1)
head.setNextNode(temp);
else
cur.setNextNode(temp);
cur = temp;
}
Node h = head;
while(h!=null)
{
System.out.print(h.getData()+",");
h = h.getNextNode();
}
head = reverse(head);
System.out.println("");
while(head!=null)
{
System.out.print(head.getData()+",");
head = head.getNextNode();
}
}
public static Node reverse(Node node)
{
Node pre = node;
Node cur = node.getNextNode();
Node next;
while(cur!=null)
{
next = cur.getNextNode();
cur.setNextNode(pre);
pre = cur;
cur = next;
}
node.setNextNode(null);
return pre;
}
}
class Node
{
int data;
Node next;
Node(int data)
{
this.data = data;
this.next = null;
}
public void setNextNode(Node node)
{
this.next = node;
}
public Node getNextNode()
{
return next;
}
public int getData()
{
return data;
}
}