Leetcode92. Reverse Linked List II反转链表
反转从位置 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反转链表的更多相关文章
- [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 ...
- [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 ...
- 92. Reverse Linked List II 反转链表 II
网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...
- 092 Reverse Linked List II 反转链表 II
反转从位置 m 到 n 的链表.用一次遍历在原地完成反转.例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4-> ...
- Leetcode92: Reverse Linked List II 翻转链表问题
问题描述 给定一个链表,要求翻转其中从m到n位上的节点,返回新的头结点. Example Input: 1->2->3->4->5->NULL, m = 2, n = 4 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- [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-> ...
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回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 ...
随机推荐
- NX二次开发-UFUN所有对象类型的宏定义
/**************************************************************************** Copyright (c) 2010 Sie ...
- c++-文件分离
实现文件分离 1.头文件中不要使用using namespace,由于c++编译的特性,由于初学还没深入了解,不做具体编译的解释 2.由于没有了命名空间,所以string定义要写成std::strin ...
- HDU1595-find the longest of the shortest-dijkstra+记录路径
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...
- ashx后门[转]
https://www.cnblogs.com/Fluorescence-tjy/p/9855828.html 一.标准ASPX一句话木马 .NET平台下的一句话木马则百年不变,最常见的当属下面这句 ...
- python爬虫_从零开始破解js加密(一)
除了一些类似字体反爬之类的奇淫技巧,js加密应该是反爬相当常见的一部分了,这也是一个分水岭,我能解决基本js加密的才能算入阶. 最近正好遇到一个比较简单的js,跟大家分享一下迅雷网盘搜索_838888 ...
- 【python】collections的使用
老师布置了一个课后作业. 统计文本中字母出现的次数并找到最大的那一个 首先是读取文本吧.和c里的也差不多,打开,关闭,读取. path = f = f.close() 然后就用到了这个黑科技.coll ...
- Lucene TFIDFSimilarity评分公式详解
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/zteny/article/details/ ...
- 使用OCCI操作Oracle数据库写入中文乱码
解决方法如下: oracle::occi::Environment *pOracleOcciEnv = Environment::createEnvironment(oracle::occi::Env ...
- .net 接受请求过来的流
//接收POST过来的数据 System.IO.Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024 ...
- 如何使用flow进行静态类型检查
Flow 是 facebook 出品的 JavaScript 静态类型检查⼯具.Vue.js 的源码利⽤了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. 为什么⽤ Flow? ...