反转从位置 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(hdu6582)Path 最短路/网络流

    题意: 在无向图上删边,让此图上从起点到终点的最短路长度变大,删边的代价是边长,求最小代价. 题解: 先跑一遍迪杰斯特拉,求出所有点的d[]值,然后在原图上保留所有的边(i,j)仅当i,j满足d[j] ...

  2. fedora23上安装和运行MySQL server (MySQL 已经被MariaDB取代)

    [root@localhost kemin]# dnf install mysql-server Fedora 23 - x86_64 - Updates                        ...

  3. 堡垒机介绍及实现 (使用python django实现)(一)

    堡垒机介绍及实现 (使用python django实现)(一) 堡垒机的功能 我们在使用服务器的时候,通常的方式是 ssh user@ip 然后输入password 多人同时使用,就需要多个账号.这时 ...

  4. BackgroundWorker study

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 什么是PoE、PSE、PD设备?

    一个完整的PoE系统包括供电端设备(PSE, Power Sourcing Equipment)和受电端设备(PD, Power Device)两部分.PSE设备是为以太网客户端设备供电的设备,同时也 ...

  6. LeetCode 67. Add Binary【个位补0,不必对齐】【easy】

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  7. drools语法介绍

    这里没有翻译http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html_single/index.html上的内容 而是参考了网 ...

  8. Activiti学习笔记11 — 判断节点的使用

    一. 创建流程 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns=" ...

  9. POST提交数据之---Content-Type的理解

    Content-Type是指http/https发送信息至服务器时的内容编码类型,contentType用于表明发送数据流的类型,服务器根据编码类型使用特定的解析方式,获取数据流中的数据. 在网络请求 ...

  10. Dubbo的底层实现原理和机制

    –高性能和透明化的RPC远程服务调用方案 –SOA服务治理方案 Dubbo缺省协议采用单一长连接和NIO异步通讯, 适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况