链表逆序
2012-10-22 17:24
162 查看
struct ListNode |
2 | { |
3 | void* m_nKey; |
4 | ListNode* m_pNext; |
5 | }; |
01 | ListNode* ReverseIteratively(ListNode* pHead) |
02 | { |
03 | ListNode* pReversedHead = NULL; |
04 | ListNode* pNode = pHead; |
05 | ListNode* pPrev = NULL; |
06 | while(pNode != NULL) |
07 | { |
08 | // get the next node, and save it at pNext |
09 | ListNode* pNext = pNode->m_pNext; |
10 |
11 | // if the next node is null, the currect is the end of original |
12 | // list, and it's the head of the reversed list |
13 | if(pNext == NULL) |
14 | pReversedHead = pNode; |
15 |
16 | // reverse the linkage between nodes |
17 | pNode->m_pNext = pPrev; |
18 |
19 | // move forward on the the list |
20 | pPrev = pNode; |
21 | pNode = pNext; |
22 | } |
23 | } |
01 | ListNode* reverse_list( ListNode* head) //逆序 |
02 | { |
03 | ListNode* new_head=head; |
04 | if(head==NULL || head->next==NULL) |
05 | return head; |
06 | new_head = reverse_list(head->next); |
07 | head->next->next=head; |
08 | head->next=NULL; //防止链表成为一个环,这是最关键的。 |
09 | return new_head; |
10 | } |
相关文章推荐
- 单链表逆序——华三、Intel面试题之一
- 典型的几个链表操作-逆序和重排
- C数据结构链表的插入,删除,逆序…
- 单链表逆序反转
- 逆序链表小范例
- 单链表逆序
- 链表逆序
- 单向链表逆序
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
- 基本算法—链表逆序
- c++实现单链表逆序
- 单链表逆序
- 链表的创建、逆序显示、插入显示
- 链表逆序和链表翻转
- 已知链表的头结点head,写一个函数把这个链表逆序
- 删除链表中的重复节点、剩余节点逆序输出
- 数据结构实验之链表二:逆序建立链表
- 创建逆序输入值的单链表
- [记着玩]java模拟链表逆序输出结果
- 2016年12月21日学习总结----单向循环链表操作程序(头插,尾插,中间插入,删除,逆序)