链表的就地逆置(递归+非递归)
2014-04-19 17:01
239 查看
#include <stdio.h> #include <stdlib.h> typedef struct ListNode { int v; struct ListNode * next; }node; node * add(node * head,int x) { node * p = head; node * r = (node*)malloc(sizeof(node)); r->next=NULL; r->v=x; if(!p) return r; while(p->next!=NULL) { p=p->next; } p->next=r; return head; } void print(node* head) { node * p=head; while(p) { printf("%d\n",p->v); p=p->next; } } node * revers(node * head) { node * p = head; if(!p) return NULL; p = head->next; head->next=NULL; while(p) { node * tmp = p->next; p->next=head; head = p; p = tmp; } return head; } void printl(node* head) { if(!head) return; if(head->next) printl(head->next); printf("%d\n",head->v); } node * revc(node * head,node * old) { if(!head) return head; if(!head->next) { head->next=old; return head; } if(head->next) { node * tmp = head->next; head->next=old; return revc(tmp,head); } } int main() { node * head=NULL; for(int i=0;i<10;i++) head = add(head,i); print(head); head = revers(head); print(head); head =revc(head,NULL); print(head); return 0; }revers是非递归的,revc是递归的。
相关文章推荐
- centos ssh命令找不到
- Struts2+Spring+Hibernate step by step 06 整合Hibernate
- Introdution to 3D Game Programming With DirectX11 第11章 习题解答
- VS2010 VS2012版最常用的快捷键
- Jquery和ajax开发案例之---自动补全输入框
- ubuntu网络连接图标不显示
- HA Cluster—heartbeat v2基于crm配置
- Sqlserver 锁表处里
- Java方法继承、方法重载、方法覆盖小总结
- HDU 3371 Connect the Cities (最小生成树 并查集+克鲁斯卡尔)
- 【二叉树&层次遍历】Populating Next Right Pointers in Each Node
- 相交线
- 鸟哥学习笔记三(基础篇第八章)
- Linux 虚拟网卡配置
- 查找算法
- netbeans 7.4中文显示乱码的解决方法
- hdu1010
- 背包问题
- SE 2014年4月18日
- 在.c文件中调用c++定义的函数