题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode *resolve(ListNode *pHead)
{
if (!pHead)
return NULL;
ListNode *pNextNode = resolve(pHead->next);
if (!pNextNode)
{
return pHead;
}
else
{
pNextNode->next = pHead;
pHead->next = NULL;
return pHead;
}
}
ListNode* ReverseList(ListNode* pHead) {
ListNode *ret = pHead;
while (ret && ret->next)
{
ret = ret->next;
}
resolve(pHead);
return ret;
}
};
思路:
首先定位最后一个子节点: 自然是while和递归中的递;
三奇智元机器人科技有限公司公司福利 50人发布