解法一:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head){ if(head==NULL) return NULL; struct ListNode* prev=NULL; struct ListNode* cur=head; while(cur) { struct ListNode* tmp=cur->next; cur->next=prev; prev=cur; cur=tmp; } return prev; }
解法二:
struct ListNode* reverseList(struct ListNode* head){ if(head==NULL||head->next==NULL) return head; struct ListNode* newHead=reverseList(head->next); head->next->next=head; head->next=NULL; return newHead; }