链表的反转
链表的翻转常见的解决方法分为迭代和递归两种。
github源码
递归和迭代两个函数在SingleLinkedList文件夹中的SingleLinkedList.c中实现。
假设创建好的链表结构如下:
迭代方式
1 | static int reverseList1(SingleLinkedList *This){ |
代码执行过程分析:
开始:
第1步:
第2步:
第3步:
第4步:
第5步:
第6步:
结束:
递归方式
1 | //递归方式 |
代码执行过程分析:
第1层递归:
第2层递归:
第3层递归:
返回到第2层:
返回到第1层:
结束:
运行测试
编译:
1 | gcc SingleLinkedList.c testSingleLinkedList.c -o testSingleLinkedList |
运行testSingleLinkedList,结果:
1 | list is empty:1 |
本文标题:链表的反转
文章作者:Mr Bluyee
发布时间:2018-10-25
最后更新:2019-07-15
原始链接:https://www.mrbluyee.com/2018/10/25/%E9%93%BE%E8%A1%A8%E7%9A%84%E5%8F%8D%E8%BD%AC/
版权声明:The author owns the copyright, please indicate the source reproduced.