class ListNode{
int data;
ListNode next;
}
public class Link{
public static ListNode ReverseLink(ListNode first){
ListNode cur = first; //一条以cur开头的链表
ListNode node = null;
ListNode result = null; //一条以result开头的链表
while(cur != null){
node = cur; //从原来链表中头删(光摘下,没有真正删除)
cur = cur.next;
node.next = result; //node 就是被头删下来的节点
result = node;
}
return result;
}
public static void print(ListNode head){
while(head != null){
System.out.println(head.data);
head = head.next;
}
}
public static void main(String[] args) {
ListNode n1 = new ListNode();
ListNode n2 = new ListNode();
ListNode n3 = new ListNode();
ListNode n4 = new ListNode();
n1.data = 1;
n2.data = 2;
n3.data = 3;
n4.data = 4;
n1.next = n2;
n2.next = n3;
n3.next = n4;
print(ReverseLink(n1));
}
}