输入:head = [1,2,3,4,5] 输出:[5,4,3,2,1]
输入:head = [1,2] 输出:[2,1]
示例 3:
输入:head = [] 输出:[]
简单题 不用画图就出来了属于是 作业好多数据结构的文章还在创作中
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* prev = NULL;
struct ListNode* cur = head;
while(cur){
struct ListNode* temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
return prev;
}
递归,好家伙这个想了好半天确实不好理解!
struct ListNode* reverseList(struct ListNode* head) {
if(NULL == head || NULL == head->next){
return head;
}
struct ListNode* ret = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return ret;
}