单链表倒着输出怎么写?敢求天下高人帮忙小弟写下
单链表倒着输出怎么写?敢求天下高人帮忙小弟写下
如:1 3 5 7 9
输出:9 7 5 3 1
[解决办法]
void reverse_print(LNode* head){ if(NULL == head) return; reverse_print(head->next); print(head->data);}
[解决办法]
Node* reverse(Node * node)
{
Node * p1 = NULL;
Node * p2 = node;
Node * p3 = node->next;
while(p3)
{
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
return p2;
}
[解决办法]
void Reverse(tNODE **pHead)
{
tNODE *pTemp1, *pTemp2, *pTail = NULL;
if(*pHead == NULL || (*pHead)->pNext == NULL)//没有节点或只有一个节点
return ;
pTemp1 = *pHead;
pTemp2 = *pHead;
while(pTemp1 != NULL)
{
pTemp2 = pTemp1;
pTemp1 = pTemp1->pNext;
pTemp2->pNext = pTail;
pTail = pTemp2;
}
*pHead = pTail;
return ;
}
[解决办法]
一楼递归漂亮。
或者可以写个使链表反序函数
void Turn_list( Linklist &L)
{
Linklist p=NULL,q=NULL;
p = (L->next)->next; //p为第二个结点
q = p->next; //q为第三个结点
L->next->next = NULL;
while( q )
{
p->next = L->next;
L->next = p;
p = q;
q = p->next;
}
p->next = L->next;
L->next = p;
}
[解决办法]