🏆个人主页:企鹅不叫的博客
🌈专栏
⭐️ 博主码云gitee链接:代码仓库地址
⚡若有帮助可以【关注+点赞+收藏】,大家一起进步!
💎一、题目
🏆1.题目描述
🏆2.原题链接
💎二、解题报告
🏆1.思路分析
🔑思路:
1.用头节点依次链接
2.无头节点依次翻转链表
🏆2.代码详解
struct ListNode* reverseList(struct ListNode* head){
//无头节点翻转
if(head == NULL){
return NULL;
}
struct ListNode* cur = head;
struct ListNode* next = cur->next;
struct ListNode* newhead =NULL;
while(cur != NULL){
cur->next = newhead; //1
newhead = cur;
cur = next; //2
if(next!=NULL){ //3
next = next->next;
}
}
return newhead;
}
1.首先把当前节点置空
2.然后依次移动cur和newhead
3.移动next要判断是否为空,防止越界