逆序链表--递归思路
2013-03-27 00:00
239 查看
今天同学问的问题,猛然一想,还不太容易想到,记录下吧。。。
#include <iostream> using namespace std; struct ListNode{ int m_nKey; ListNode* m_pNext; }; bool creatList(ListNode *&head,int *index,int length){ if(length<0){ return false; } ListNode *pPre = head; for(int i = 0 ; i < length ; ++i){ ListNode *pNewListNode = new ListNode; pNewListNode->m_nKey = index[i]; pNewListNode->m_pNext = NULL; pPre->m_pNext = pNewListNode; pPre = pNewListNode; } return true; } ListNode * re(ListNode *pCurrent,ListNode *&head){//递归逆序链表 if(pCurrent->m_pNext == NULL){ head = pCurrent; return pCurrent; } ListNode * pTemp = re(pCurrent->m_pNext,head); pTemp->m_pNext = pCurrent; return pCurrent; } void disPlay(ListNode *head){ while(head != NULL){ cout << head->m_nKey << " "; head = head->m_pNext; } cout << endl; } int main(){ int index[5] = {1,2,3,4,5}; ListNode* pHead = new ListNode; ListNode* pLast; creatList(pHead,index,5); re(pHead->m_pNext,pHead->m_pNext)->m_pNext = NULL; disPlay(pHead->m_pNext); system("pause"); return 1; }
相关文章推荐
- 用递归实现链表逆序
- 链表逆序的循环实现和递归实现
- 链表逆序(递归&非递归)/倒序输出链表值/用一个递增序列构建平衡二叉搜索树/用递归的方法判断数组是不是升(降)序排列
- 链表递归逆序输出和反转链表。
- 链表面试题--两种方法逆序打印单链表(栈和递归)
- 链表逆序输出(递归妙)
- 练练手,练练手——C语言用递归对链表逆序
- 链表逆序(递归实现和非递归实现)
- 递归实现链表逆序(不需三个临时指针变量)
- 给定的单链表逆序操作的递归与非递归算法总结
- 链表逆序输出(递归妙)
- 带头结点链表逆序---逆归与非递归实现
- 链表逆序输出(递归妙)
- C++ 递归和非递归实现链表逆序
- C/C++用递归的方法求链表逆序
- 单链表逆序:通过递归和循环实现。
- 程序员面试宝典之数据结构基础---⑤单链表逆序的递归与非递归实现
- 通过迭代(非递归)及递归将单链表逆序
- 单链表逆序循环和递归2种方式
- 单链表逆序的递归与非递归算法