输入一个链表,反转链表后,输出链表的所有元素。
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null){
return null;
}
ListNode hea=new ListNode(0);
hea.next=head;
ListNode p1,p2;
p1=head;
p2=head.next;
while(p1.next!=null){
p1.next=p2.next;
p2.next=hea.next;
hea.next=p2;
p2=p1.next;
}
return hea.next;
}
}