/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head||!head->next) return head; //如果链表个数小于等于1,则输出头结点
auto p=head, q=head->next;
while(q)
{
auto o=q->next;
q->next=p;
p=q;
q=o;
}
head->next=NULL; //此时head为尾结点,应该指向空
return p; //最后新的头结点为p
}
};