<pre name="code" class="cpp">/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ListNode* reverseList(ListNode* head)
{
if(!head)
return head;
ListNode* p=head;
ListNode* q=p->next;
if(!q)
return head;
ListNode* k=q->next;
p->next=NULL;
while(k)
{
q->next=p;
p=q;
q=k;
k=k->next;
}
q->next=p;
return q;//返回头结点
}