单向链表的反转


haha!


public class Node
{
String content;

Node next;

public Node(String s) {
content = s;
}

public String toString() {
return content + ":next=" + next;
}

public static Node convert(Node head) {
Node nex = null;
Node cur = head.next;
Node pre = head;
while (cur != null)
{
nex = cur.next;
cur.next = pre;
pre = cur;
cur = nex;

}
head.next = null;
return pre;
}

public static void main(String[] args)
{
Node[] ns = new Node[10];
for(i=0;i<10;i++)
{

}
for (int i=0;i<10;i++)
{
ns[i] = new Node("" + i);
}
for (int i=0;i<9;i++)
{
ns[i].next = ns[i + 1];
}
System.out.println(ns[0]);
System.out.println(convert(ns[0]));
}
}


没有评论: