Reverse Linked List II
/**
* 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) {
ListNode * res=head;
if(m==n) return res; int i=;
ListNode * pre=NULL; //记录待反转链表的前驱,注意,带反转链表从头开始的情况,没有前驱
ListNode * left; //记录待反转链表的开始
if(m>) {
pre=head;
while(i<m-){
pre=pre->next;
i++;
}
left = pre->next;
}
else left = head; i=;
ListNode *post=left; //记录待反转链表的后继,注意赋初值为left,而不是head,pre之类的
while(i<n-m+){
post=post->next;
i++;
} ListNode *right=reverselist(left,n-m+); //记录待反转链表的结束 if(pre) pre->next =right;
else res = right; while(right->next){
right=right->next;
} right->next=post;
return res;
} //把head开始长度为l的拉链,反转
ListNode* reverselist(ListNode* head,int l)
{
ListNode * pre=NULL;
ListNode * cur=head;
ListNode * post=NULL;
int i=; while(i<l){
post = cur->next;
cur->next = pre;
pre = cur;
cur = post;
i++;
}
return pre;
}
};
Reverse Linked List II的更多相关文章
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- 92. Reverse Linked List II
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- lc面试准备:Reverse Linked List II
1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...
- 【LeetCode练习题】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- leetcode -day30 Reverse Linked List II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one- ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- Bash中的shopt选项
Bash中的shopt选项 http://blog.chinaunix.net/uid-20587169-id-1919110.html shopt命令用于显示和设置shell中的行为选项,通过这些选 ...
- 在C#中使用json字符串
http://jingyan.baidu.com/article/6fb756ecd2b051241858fbef.html
- C# Main函数的 args参数
网上参考 博客,使用如下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 微信的公众号unionid
此外,由于开发者经常有需在多个平台(移动应用.网站.公众帐号)之间共通用户帐号,统一帐号体系的需求,微信开放平台(open.weixin.qq.com)提供了UnionID机制.开发者可通过OpenI ...
- The Struts dispatcher cannot be found. This is usually caused by using Strut
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc ...
- javax.xml.ws.webserviceexception class do not have a property of the name
我是用wsimport生成webservice 的客户端,放到工程里,调用,出现这个异常, 后来发现,是没有把package-info.java这个文件一起放到包里的缘故 解决: 连同package- ...
- iphone 语音备忘录 同步问题
iphone 是很人性化的,但itune是反人类的. 我想同步电话里的语音备忘录,结果有几个记录在itunes里面是不显示的,无法同步出来. 找了很多解决方法,最后找了ifunbox才搞定.
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(5)
12.5 创建表和发送查询 介绍了Postgres-XC以及其底层的思想之后,是时候创建我们的第一个表,看看集群将如何表现.下面的例子演示了一个简单的表.将使用id列的哈希键来分布它: test=# ...
- @perproty and @synthesize
.@property 是什么? @perperty 是声明属性的语法,他可以快速方便的为实例变量创建存取器,并允许我们通过点语法使用存取器 [存取器:用于获取和设置实例变量的方法,获取实例变量值得是g ...
- FAQ: c++ 函数名后添加 const void function1(int &id) const
说明这个函数不能修改这个类的成员变量!只能操作自己的参数和内部的范围变量! 括号内的&id,&表示这个变量和C# in和out是一样的,算是一个reference,可以更改值,要想不更 ...