leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
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->NULL, m = 2 and n =
4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
处理这个问题还是挺复杂的。须要考虑非常多边界的測试用例。我整体的思路是先用循环标记m前一个节点和n后边一个节点。把n后边的节点首先作为当前逆转节点的pre,然后循环n-m次完毕所选节点部分的逆序,然后将标记的m节点前一个节点指向逆序后部分的头节点就可以。
要考虑各种特殊情况,另外考虑就可以。
code例如以下:
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
if(head==NULL || m<0 || n<0)
return head;
if(head->next == NULL || m==n)
return head;
ListNode *head2=NULL,*pre,*cur,*temp=head;
for(int i=0; i<n+1; i++)
{
if(i==m-2)
head2=temp;
else if(i==m-1)
cur=temp;
else if(i==n)
pre=temp;
if(temp!=NULL)
temp = temp->next;
}
for(int i=m;i<n+1;i++)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
if(m==1)
return pre;
head2->next = pre;
return head;
}
};leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)的更多相关文章
- [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-> ...
- [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 ...
- 【原创】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-p ...
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
- [LC]237题 Delete Node in a Linked List (删除链表中的节点)(链表)
①中文题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: hea ...
- 【LeetCode】24.两两交换链表中的节点
24.两两交换链表中的节点 知识点:链表 题目描述 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点.你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换). 示例 示例 1 ...
- [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 ...
- [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-> ...
随机推荐
- I帧、P帧和B帧的特点
I帧:帧内编码帧 I帧特点: 1.它是一个全帧压缩编码帧.它将全帧图像信息进行JPEG压缩编码及传输; 2.解码时仅用I帧的数据就可重构完整图像; 3.I帧描写叙述了图像背景和运动主体的详情; 4.I ...
- POJ 1006 Biorhythms (数论-中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 111285 Accepted: 34638 Des ...
- 【BUG】"main" prio=5 tid=1 RUNNABLE
载入超大效果图导致内存不足(GC/ANR) 06-30 11:42:56.624: D/dalvikvm(16264): GC_CONCURRENT freed 1982K, 7% free 4537 ...
- Python!Are you kidding me?
前几天由于python给我带来了兴奋写了一篇文章叫做<The beauty of python 1>,今天则是由于一个小错误而写下此文. 也是缘因为我的工作,问题是这种: 我有一个文档.里 ...
- 迷茫了好一阵决定做WEB前端
前两个学期事实上总是每一个学期给自己做一个计划.可是计划都付诸流水,不是自己不坚持,仅仅由于目标太不明白,总是不见成效.前一段时间最终感觉计划还得做,可是不能超过一个月,要把计划做到仔细到每一周每一天 ...
- vim 基础学习之查找
普通模式下 /->正向查找 n-向下查找 N-向上查找 ?->反向查找 N-向下查找 n-向上查找 <C-r><C-w> <C-r>-引用,例如引用寄存 ...
- Vue绑定事件
<!-- 方法处理器 --> <button v-on:click="doThis"></button> <!-- 内联语句 --> ...
- Fragment-如何监听fragment中的回退事件与怎样保存fragment状态
一.如何监听Fragment中的回退事件 1.问题阐述 在Activity中监听回退事件是件非常容易的事,因为直接重写onBackPressed()函数就好了,但当大家想要监听Fragment中的回退 ...
- 跨域使用onmessage实现方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BootStrap_Table 学习
https://blog.csdn.net/heting90/article/details/52248729 $("#realTime_Table").bootstrapTabl ...