Leetcode139: Reverse Linked List II
2015-11-04 22:36
281 查看
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.
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.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { if (head == NULL) return NULL; ListNode *q = NULL; ListNode *p = head; for(int i = 0; i < m - 1; i++) { q = p; p = p->next; } ListNode *end = p; ListNode *pPre = p; p = p->next; for(int i = m + 1; i <= n; i++) { ListNode *pNext = p->next; p->next = pPre; pPre = p; p = pNext; } end->next = p; if (q) q->next = pPre; else head = pPre; return head; } };
相关文章推荐
- Android上dip、dp、px、sp等单位说明(转)
- Fiddler 教程
- 监听器
- DevOps有关的书
- Redis学习手册(Set数据类型)
- 字符串和对应的整数之间的转换
- 将博客搬至CSDN
- AsyncHttpClient 、HttpURLConnection get/post请求、httpClient
- 将输入的字符串中的小写字母全部转化为大写字母
- Wireshark基本介绍和学习TCP三次握手
- 1040 有几个PAT
- 彻底理解JavaScript原型
- ecshop 会员登录注册业务逻辑实现
- tomcat配置多项目JAR包共享
- sourceTree 使用前准备
- LightOJ - 1300 Odd Personality(边双连通+奇圈判定)
- Linux PS 命令详解
- [TwistedFate]中等通讯录
- HOG(Histogram of Oriented Gradient)方法简介
- 1039 到底买不买