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 ...
随机推荐
- Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .
String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...
- 使用sh-x调试shell脚本_转
参考:http://blog.chinaunix.net/uid-20564848-id-73502.html 1. 通过sh -x 脚本名 #显示脚本执行过程2.脚本里set -x选项,轻松跟踪调 ...
- H3C S3600-28TP-SI配置命令
模式分类为:<H3C> 用户视图 [H3C] 系统视图 [H3C-Ethernet1/0/1] 以太网端口视图 [H3C-vlan10] VLAN视图 [H3C-Vl ...
- Janus WinForms Controls
http://blog.sina.com.cn/s/blog_68eb92020101kpw8.html
- 手动内存管理与ARC互相转换问题2
- 查看python的路径
>>> import sys >>> sys.path
- python AES 双向对称加密解密
高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...
- LIS 最长递增子序列
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
- .net 中 ref out params的区别
C#中有三个关键字-ref,out ,params,虽然本人不喜欢这三个关键字,因为它们疑似破坏面向对象特性.但是既然m$把融入在c#体系中,那么我们就来认识一下参数修饰符ref,out ,param ...
- python在window下的Nginx部署
Python版本3.21 安装nginx下载windows上的nginx最新版本,http://www.nginx.org/en/download.html.解压后即可.运行nginx.exe后本地打 ...