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->NULLm =
2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn 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的更多相关文章

  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 ...

  2. [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-> ...

  3. 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-> ...

  4. [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 ...

  5. 【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-> ...

  6. 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-> ...

  7. 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-> ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Windows 环境下安装 Oracle JDK

    本页面中描述了如何在 Window 环境下安装 Oracle JDK. 我们使用的版本是 Window 10,我们需要安装的版本是 Oracle JDK 8u191. 检查当前版本 在进行新的 JDK ...

  2. P3226 [HNOI2012]集合选数

    考虑构造矩阵 1 3 9 27...... 2 6 18 54...... 4 12 36 108...... ...... 发现在这个矩阵上一个合法的集合是一个满足选择的数字不相邻的集合,由于行数列 ...

  3. laravel时间判断

    $now = Carbon::now(); if ($now >= '2019-01-02') { }

  4. Laravel JsonResponse数组获取

    有一个JsonResponse数据的格式如下: object(Illuminate\Http\JsonResponse)[474] protected 'data' => string '{&q ...

  5. python-day3笔记

    1.通信是软件(计算机)与软件(计算机)之间的通信 2.网络指的是: 一:计算机与计算机之间通过物理连接介质(网络设备)连接到一起:光纤--物理连接介质,和网线一样. 二:计算机与计算机之间基于网络协 ...

  6. 安全模式下卸载windows installer打包的软件(转)

    安全模式下卸载windows installer打包的软件 起因: 主机系统MAC,虚拟软件Parallels Desktop, 虚拟系统 Win 7. 今天在虚拟机WIN7里面安装了某个软件导致重启 ...

  7. Google浏览器设置变更默认搜索引擎为百度

  8. textAlign

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  9. javascript里的偏函数——本质函数式编程+闭包,返回函数

    最终效果: var greet = function(greeting, name) { return greeting + ' ' + name; }; var sayHelloTo = _.par ...

  10. httpclient 多线程请求

    线程请求执行 当配备一个线程池管理器后,如PollingClientConnectionManager,HttpClient就能使用执行着的多线程去执行并行的多请求. PollingClientCon ...