Reverse Linked List II - LeetCode
2015-10-24 11:12
417 查看
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given
return
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
思路:只要会了reverse k-group,反转链表再也不用愁了呢!
For example:
Given
1->2->3->4->5->NULL, m = 2 and n = 4,
return
1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
思路:只要会了reverse k-group,反转链表再也不用愁了呢!
class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { if (head == NULL || m == n) return head; ListNode prenode(-1); prenode.next = head; ListNode *pre = &prenode, *cur, *nex; int count = m - 1; while (count && (pre = pre->next)) count--; cur = pre->next; nex = cur->next; for (int i = 1; i < n - m + 1; i++) { cur->next = nex->next; nex->next = pre->next; pre->next = nex; nex = cur->next; } return prenode.next; } };
相关文章推荐
- JavaScript实现标题栏文字轮播效果代码
- MyEclipse 2014 下载与安装教程
- 51NOD1265(四点共面)
- OpenCV—图像椒盐噪声生成器
- 常用数据库系统简介
- 基于作业互评的新式评分方案(学生,教师可参考)
- [PYTHON]-用Scrapy爬虫遍历百度贴吧,本地保存文字版【PART 2】
- Java资源目录访问一目了然
- wamp 开启mysql慢查日志
- 定制自己的内核
- 内核配置与裁剪浅析
- Java基础学习2(关键字,标识符,数据类型)
- centos 安装rpmforge、rpmfusion、epel软件仓库 ( a old article !!!!!! )
- mysql 导出 sql 执行sql
- 关于使用Xcode--Editor相关选项建立autoLayout的约束的使用介绍
- ubuntu下安装kde Plasma
- 动手动脑
- Android--调用系统的DownLoadManager去下载文件
- WPF三大模板简介
- 地理编码服务 GeoCode Service