ListNode* revertListInt(ListNode* node, ListNode* &newHead)
{
if (node && node->next)
{
revertListInt(node->next, newHead)->next = node;
}
else if (node)
{
newHead = node;
}
return node;
}
ListNode* revertList(ListNode *head)
{
if (!head)
return NULL;
ListNode *oldHead = head;
ListNode *newHead = NULL;
revertListInt(head, newHead);
oldHead->next = NULL;
return newHead;
}
int _tmain(int argc, _TCHAR* argv[])
{
ListNode n1(1);
ListNode n2(2);
ListNode n3(3);
ListNode n4(4);
ListNode n5(5);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
ListNode *newHead = revertList(&n1);
}
反转链表 (递归法)
最新推荐文章于 2025-06-04 00:01:06 发布