Reverse Linked List
2015-06-08 23:21
375 查看
Reverse a singly linked list.
思路:
to slove this problem, I choose a recurrent method, to reverse linked-list nodes one-by-one.
first, point head to NULL.(make the head be a tail)
and then, point cur node to prev node, and then, move prev pointer to cur node, move cur pointer to next node.
until next node move to NULL.
*recursive(递归),recurrent(递推)
思路:
to slove this problem, I choose a recurrent method, to reverse linked-list nodes one-by-one.
first, point head to NULL.(make the head be a tail)
and then, point cur node to prev node, and then, move prev pointer to cur node, move cur pointer to next node.
until next node move to NULL.
*recursive(递归),recurrent(递推)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if (head == NULL || head->next == NULL) { return head; } ListNode *prev = head, *cur = head->next, *next = cur->next; head->next = NULL; while (next != NULL) { cur->next = prev; prev = cur; cur = next; next = next->next; } cur->next = prev; return cur; } };
相关文章推荐
- 关于进程与线程的讲解 最最最生动的理解
- 算法导论习题-1.2-2
- Bit、 Byte_KB_MB_GB 间的关系
- 文本相似度计算基本方法小结
- 阅读JDK源码有感
- 看见 读书笔记
- MeTA is a modern C++ data sciences toolkit featuring
- startActivityForResult用法详解
- ubuntu14.04设置静态IP
- 2015年6月8日修习
- Oracle视图
- 前端开发大众手册(包括工具、网址、经验等)
- 安卓版微信自带浏览器和IE6浏览器ajax请求abort错误处理
- 下拉列表框Spinner-采用自定义布局文件作为Spinner样式
- 再回首,Java温故知新(九):Java基础之流程控制语句
- C++之编译器与链接器工作原理
- I/O口输出点亮LED
- 飘落的雪花
- ActionBar 通用方法
- 模拟链表