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 ...
随机推荐
- [转]gulp打包工具总结
与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...
- 黑科技之杜教bm
这个板子能够解决任何线性递推式,只要你确定某个数列的某项只与前几项线性相关,那么把它前若干项丢进去,这个板子就能给你返回你要求的某项的值. 原理???(待补充) #include<bits/st ...
- 在Word中如何自动生成参考文献引用
来自:在Word中如何自动生成参考文献引用 在写毕业论文时,参考文献动辄就有四五十篇,在文中对照参考文献逐一引用是一件十分痛苦的事情,而且一旦参考文献的顺序发生变化,文中的引用也要逐个修改,那么,我们 ...
- Codeforces 1182A Filling Shapes
题目链接:http://codeforces.com/problemset/problem/1182/A 思路:n为奇数时不可能完全填充,ans = 0.发现若要完全填充,每俩列可产生俩种情况,所以为 ...
- msSql Server 修复数据库
--rz要替换为修复数据库DBCC CHECKTABLE ('rz'); use master declare @databasename varchar(255) set @databasename ...
- MySQL更新指定分组中最大值记录
数据表中存储着某个状态字段,当分组中所有数据入库后,需要根据相应的最大值更新状态字段,如表: 现在要将no=11的分组中把最大值记录的status更新为1,可以直接这么写: ; done~
- SQL链接EXCEL操作
Sub CopyData_5() Set Cnn = CreateObject("ADODB.Connection")With Cnn.Provider = "micro ...
- wpf datagrid 如何自定义行的控件实例,(textbox 并选中则全选)
主要是为了用户输入方便 按回车,选中下一列,text自动获取焦点,输入状态 获取控件实例 https://blog.csdn.net/m15188153014/article/details/486 ...
- Vim模糊查找与替换
例如要把 ( 1 ).( 2 ) - 全部替换成其他字符,可以用命令: :%s/(.*)/str/gn 其中,.* 表示匹配任何东西,如果只希望匹配应为字母和数字,可以用 \w\+. 有些特殊字符需要 ...
- 专访阿里云MVP王俊杰:开发者的超能力是用技术让世界更美好
[王俊杰:阿里云MVP,陕西创博网络科技有限公司总经理.大数据与物联网的爱好者与实践者. 8年以上互联网从业经验,曾从事军工相关仿真分析软件研发与集成.4年以上大数据系统开发经验.目前正与天水市秦州区 ...