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-pass.
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.
分析:開始看题目以为是仅仅交换两个指定位置的值。后来写出代码来出错。才发现是翻转位置m和n直接的链表,我的基本思路是先用双指针法找到要翻转链表的位置,将链表分成三段,要翻转的前一段,中间呀翻转的段,和剩下的段。最后写出代码来特别繁琐。例如以下所看到的:
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
if(m < 1 || m >= n){
return head;
}
//双指针法找到要翻转的链表段
ListNode* node1 = head;
ListNode* node2 = head;
int dis = n - m;
int i = 0;
for(; i<dis && node2; ++i){
node2 = node2->next;
}
if(i<dis){
return head;
}
while(i<n-2 && node2){
node1 = node1->next;
node2 = node2->next;
++i;
}
//node3为要翻转的链表段的開始结点
ListNode* node3 = node1->next;
if(m == 1){
node1 = NULL;
node3 = head;
}else{
node1->next = NULL;
node2 = node2->next;
if(!node2){
return head;
}
++i;
}
//node4为剩下的链表
ListNode* node4 = NULL;
node4 = node2->next;
node2->next = NULL;
//假设链表长度大于n时。能够进行
if(i == n-1){
ListNode* newHead = reverseList(node3); //翻转中间链表
//连接三段链表
node3->next = node4;
if(m !=1 ){
node1->next = newHead;
return head;
}else{
return newHead;
}
}
return head;
}
ListNode* reverseList(ListNode* head){
ListNode* node1 = NULL;
ListNode* node2 = head;
ListNode* tempNode = NULL;
while(node2){
tempNode = node2->next;
node2->next = node1;
node1 = node2;
node2 = tempNode;
}
return node1;
}
};
改进:上述代码非常繁琐。搜了别人的代码,非常简洁,问题在于上述对中间链表进行了两次遍历,缩短为一次遍历。可缩减代码。上列代码没有考虑异常情况,比方链表长度<m或者<n等情况。
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
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;
}
};
leetcode -day30 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 ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Java for LeetCode 092 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-> ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- leetcode 92 Reverse Linked List II ----- java
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 a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- LeetCode 92. Reverse Linked List II倒置链表2 C++
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [leetcode]92. Reverse Linked List II反转链表2
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
随机推荐
- python-day8-赋值
# x=10 #链式赋值# a=b=c=d=e=f=10# print(a,b,c,d,e,f) #增量赋值 # x=10# y='a'# temp=x# x=y# y=temp# print(x,y ...
- windows下进程与线程剖析
进程与线程的解析 进程:一个正在运行的程序的实例,由两部分组成: 1.一个内核对象,操作系统用它来管理进程.内核对象也是系统保存进程统计信息的地方. 2.一个地址空间,其中包含所有可执行文件或DLL模 ...
- 阿里云ECS安装最新版本Node.js
原文 http://www.w3ctech.com/topic/1610 主题 Node.js操作系统服务器 我的ECS实例是Ubuntu操作系统,直接使用 apt-get install node ...
- OC id类型
id数据类型可存储任何类型的对象.从某种意义说,它是一般对象类型. -------------------------"NormalMan.h"------------------ ...
- html <frame>标签使用
标签定义 frameset 中的一个特定的窗口(框架) frameset中的每个框架都可以设置不同的属性,比如border,scrolling,noresize等 frame的常用属性 width: ...
- angular封装jquery插件(组件)
http://www.phloxblog.in/jquery-plugin-angular-js-directive-clean-html-approach/#.VaCWL9yUemJ
- SQL Server 调优系列基础篇 - 联合运算符总
前言 上两篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符的优化技巧,本篇我们总结联合运算符的使用方式和优化技巧. 废话少说,直接进入本篇的主题. 技术准备 基于SQL Server200 ...
- 浅谈:从为什么学习python到如何学好python
虽然目前的编程语言有很多,但是基础语法上的概念,本质上都是相通的.可以做到一通百通.所以没有必要为了学哪门语言纠结太多. python是目前市面上,我个人认为是最简洁&&最优雅& ...
- IoC基础例子
一个简单的例子: 一般新建一个com.dao包,存放一些dao接口. com.dao.impl里面存放具体的dao com.service存放service接口 com.service.impl具体的 ...
- Android中判断字符是否为中文、韩文、日文
我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...