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 ...
随机推荐
- Linux就这个范儿 第9章 特种文件系统
Linux就这个范儿 第9章 特种文件系统 http://book.douban.com/reading/32081222/ P326 有一种文件系统,根本不在磁盘上.这种文件系统就是大名顶顶的ram ...
- Ubuntu下virtualbox nat网络模式下 实现宿主机访问虚拟机
参考原文(在windows环境下):http://hi.baidu.com/george_gly/item/5183b76e5a79e49ac5d2498b nat网络模式下,虚拟机可以访问外网.访问 ...
- android自动填写短信验证码
广播类 package com.examp.azuoyoutong.listner; import java.util.regex.Matcher;import java.util.regex.Pat ...
- linux内核编译相关
参考:http://www.arm.linux.org.uk/docs/kerncomp.php 一. 内核编译1) linux 2.4make clean/make mrpropermake dep ...
- 发布自己的nuget包;报错source parameter was not specified
16-10-27 VS下使用 程序包管理器控制台 运行: 1.cd 命令走到 工程文件夹下,使用 ls 命令查看当前目录: 2. 使用 nuget spec 创建: 3. 使用 nuget pack ...
- 缩进, Tab 还是空格?(转)
圣战 圣战个毛线 Android 好还是 iOS 好,Windows 好还是 Mac 好,编辑器好还是 IDE好,中划线好还是下划线好…写代码这么多年,会遇到很多奇葩的毫无意义的所谓脑残粉的争论 昨天 ...
- 设置GPnP profile文件中asm spfile的位置
登录asmcmd asmcmd> spset <location-绝对路径> 示例: ASMCMD> spset +DATA/asm/asmparameterfile/asms ...
- mysql:on duplicate key update与replace into
在往表里面插入数据的时候,经常需要:a.先判断数据是否存在于库里面:b.不存在则插入:c.存在则更新 一.replace into 前提:数据库里面必须有主键或唯一索引,不然replace into ...
- Unity3D 通用提示窗口实现分析(Inventory Pro学习总结)
背景 游戏中的UI系统或者叫做GUI窗口系统主要有:主要装备窗口(背包,角色窗口也是一种特殊窗口).确实提示窗口(如购买确认).信息提示窗口(一遍没有按钮,ContexntMenu)和特殊窗口(聊天记 ...
- linux 缺少动态连接库.so--cannot open shared object file: No such file or directory
error while loading shared libraries的解決方法 执行行程式時,如此遇到像下列這種錯誤: ./tests: error while loading shared l ...