class ListNode{
int val;
ListNode next=null;
ListNode(int val){
this.val = val;
}
}
//包括主函数建立链表和关键代码两部分
public class Main{
public static void main(String[] args) {
ListNode node = new ListNode(0);
ListNode head = node ;
for (int i = 1; i < 5; i++) {
node.next = new ListNode(i);
node = node.next;
}
//0 1 2 3 4
ListNode listNode = power(head);
while(listNode!=null){
System.out.println(listNode.val);
listNode=listNode.next;
}
}
static ListNode power(ListNode head){
ListNode pre=null;
while(head!=null){
ListNode temp=head.next;
head.next=pre;
pre=head;
head=temp;
}
return pre;
}
}