方法一:递归
struct ListNode* reverseList(struct ListNode* head){
if(!head||head->next==NULL)
return head;
struct ListNode*p=reverseList(head->next);
head->next->next=head;
head->next = NULL;
return p;
}
方法二快慢指针
struct ListNode* reverseList(struct ListNode* head){
struct ListNode*tmp,*h;
tmp=(struct ListNode*)malloc(sizeof(struct ListNode));
tmp->next=NULL;
while(head!=NULL)
{
h=(struct ListNode*)malloc(sizeof(struct ListNode));
h->val=head->val;
head=head->next;
h->next=tmp->next;
tmp->next=h;
}
tmp=tmp->next;
return tmp;
}