C语言
方法普通,内存消耗大
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
if(head==NULL)
{
return head;
}
struct ListNode* p=head;
struct ListNode* q=head->next;
head->next=NULL;
while(q!=NULL)
{
struct ListNode* r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return p;