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 ...
随机推荐
- MariaDB 服务器在 MySQL Workbench 备份数据的时候出错如何解决
服务器是运行在 MariaDB 10.2 上面的,在使用 MySQL Workbench 出现错误: mysqldump: Couldn't execute 'SELECT COLUMN_NAME, ...
- 『OpenCV3』Harris角点特征_API调用及python手动实现
一.OpenCV接口调用示意 介绍了OpenCV3中提取图像角点特征的函数: # coding=utf- import cv2 import numpy as np '''Harris算法角点特征提取 ...
- 『Json』常用方法记录
json模块可以把字典结构改写为string然后保存,并可以反向读取字典 pickle模块则可以持久化任意数据结构 但是即使同样是字典数据结构,两个包也是有差别的, json字典value不支持其他对 ...
- IDEA搭建ssm框架测试衍生出的问题The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Develop\jdk7\jdk1.7.0_79\bin;
最近玩起IDEA这开发工具,搭建ssm框架测试时,部署项目出现如下问题: 信息: The APR based Apache Tomcat Native library which allows opt ...
- yield 关键字
yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...
- Eclipse用了官方汉化后,无法输入
解决方法:Rclipse右键→属性→兼容性→windows vista
- 常见MIME类型例表
常见MIME类型例表: 序号 内容类型 文件扩展名 描述 1 application/msword doc Microsoft Word 2 application/octet-stream bin ...
- vue中上传文件相同文件名没反应
vue项目中会遇到上传文件的需求,jquery会有一些插件很方便,如果不使用插件网上的方法没有太容易的而且很多是原生JS或者基于jQuery操作dom结构的.那么在vue项目中如何实现呢,还有如何模拟 ...
- spring cloud 学习(一)初学SpringCloud
初学SpringCloud 前言 在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了.主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…… ...
- sha256 in C language
sha256.h #ifndef _SHA256_H#define _SHA256_H #ifndef uint8#define uint8 unsigned char#endif #ifndef u ...