/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode*j=pHead;
int size=0;
while(j!=nullptr)
{
size++;
j=j->next;
}
if(size<=1)
{
return pHead;
}
if(size==2)
{
ListNode*p=pHead->next;
pHead->next=nullptr;
p->next=pHead;
return p;
}
ListNode*p=pHead;
ListNode*q=p->next;
ListNode*s=q->next;
if(p==pHead)
{
// p->next=NULL;
p->next=nullptr;
}
q->next=p;
p=q;
while(s)
{
q=s;
s=q->next;
q->next=p;
p=q;
}
return p;
}
};