/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode h=new ListNode();
ListNode p,t;
if(head==null) return null;
if(head.next==null) return head;
h.next=head;
p=head.next;
t=p.next;
head.next=null;
while(p!=null){
p.next=h.next;
h.next=p;
p=t;
if(p!=null)
t=p.next;
}
return h.next;
}
}