反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

说明:

1 ≤ m ≤ n ≤ 链表长度。

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4 输出: 1->4->3->2->5->NULL

把从m到n的反转,

然后再接上去。

 class Solution
{
public:
ListNode * reverseBetween(ListNode* head, int m, int n)
{
int cnt = 0;
ListNode* ansHead = NULL;
ListNode* reverseHead = NULL;
ListNode* lastNode = NULL;
ListNode* tail = NULL;
while (head != NULL)
{
ListNode *node = new ListNode(head->val);
cnt++;
if (cnt == m && m != n)
{
tail = lastNode;
if (reverseHead == NULL)
{
reverseHead = node;
lastNode = reverseHead;
head = head->next;
}
while (head != NULL)
{
ListNode *node = new ListNode(head->val);
cnt++;
node->next = lastNode;
lastNode = node;
head = head->next;
if (cnt == n)
{
break;
}
}
if (tail == NULL)
{
ansHead = lastNode;
lastNode = reverseHead;
}
else
{
tail->next = lastNode;
lastNode = reverseHead;
}
}
else
{
if (ansHead == NULL)
{
ansHead = node;
lastNode = ansHead;
}
else
{
lastNode->next = node;
lastNode = node;
}
head = head->next;
}
}
return ansHead;
}
};

Leetcode92. Reverse Linked List II反转链表的更多相关文章

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

  2. [Leetcode] Reverse linked list ii 反转链表

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...

  3. 92. Reverse Linked List II 反转链表 II

    网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...

  4. 092 Reverse Linked List II 反转链表 II

    反转从位置 m 到 n 的链表.用一次遍历在原地完成反转.例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4-> ...

  5. Leetcode92: Reverse Linked List II 翻转链表问题

    问题描述 给定一个链表,要求翻转其中从m到n位上的节点,返回新的头结点. Example Input: 1->2->3->4->5->NULL, m = 2, n = 4 ...

  6. [LeetCode]92. Reverse Linked List II反转部分链表

    /* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...

  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. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

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

随机推荐

  1. hdu多校第九场 1005 (hdu6684) Rikka with Game 博弈

    题意: 给一个小写字母组成的字符串,每回合轮到某人时,此人可以选择让某位+1(如果是z则变回a),或者直接结束游戏. 先手希望游戏结束时字符串字典序尽量小,后手希望游戏结束时字符串字典序尽量大,求游戏 ...

  2. 李宏毅机器学习课程---2、Regression - Case Study

    李宏毅机器学习课程---2.Regression - Case Study 一.总结 一句话总结: 分类讨论可能是比较好的找最佳函数的方法:如果 有这样的因素存在的话 模型不够好,可能是因素没有找全 ...

  3. 神经网络中使用Batch Normalization 解决梯度问题

    BN本质上解决的是反向传播过程中的梯度问题. 详细点说,反向传播时经过该层的梯度是要乘以该层的参数的,即前向有: 那么反向传播时便有: 那么考虑从l层传到k层的情况,有: 上面这个 便是问题所在.因为 ...

  4. NodeJS学习笔记之Connect中间件模块(二)

    一,开篇分析 大家好,今天这篇文章主要是对"Connect"中间件以及相关辅助中间件,做一个源码分析系列,我想上一篇文章大家也看了, 介绍了使用方式及用途,而这篇也是出于本人的兴趣 ...

  5. linux安装openoffice,并解决中文乱码

    1.安装openoffice 官网http://www.openoffice.org/zh-cn/download/下载 2.解压并进入文件夹: cd /zh-cn/RPMS yum localins ...

  6. Impala简介

  7. C 终端输入 字符123 输出 10进制123

    #include <stdio.h> #define N 20 int main(int argc, const char *argv[]) { char a[N] = {'\0'}; i ...

  8. 幂等 zuul的Filter实现

    通过zuul的过滤器 filter实现 //app 幂等过滤 @SuppressWarnings("all") @Order(Ordered.HIGHEST_PRECEDENCE) ...

  9. css----less预处理器

    ###less less是一种动态样式语言,属于css预处理器的范畴,它扩展了 CSS 语言, 增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展 LESS 既可以在 客户端 上运行 ,也 ...

  10. 微信小程序传递URL中含有特殊字符

    小程序传递URL中含有特殊字符"="时,解决办法:先encodeURIComponent,取到值以后再decodeURIComponent 首先在A页面 var urls = en ...