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)的更多相关文章

  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:Given 1-> ...

  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. 【原创】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 ...

  4. lintcode 中等题: reverse linked list II 翻转链表II

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

  5. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

  6. [LC]237题 Delete Node in a Linked List (删除链表中的节点)(链表)

    ①中文题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: hea ...

  7. 【LeetCode】24.两两交换链表中的节点

    24.两两交换链表中的节点 知识点:链表 题目描述 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点.你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换). 示例 示例 1 ...

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

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

随机推荐

  1. Python命令行參数大全

      -b     :    当转换数组为字符串时提出警告.比方str(bytes_instance), str(bytearray_instance). -B     :    当导入.py[co]文 ...

  2. Effective C++ Item 38 通过复合塑模出 has-a 或 is-implemented-in-terms-of

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:在应用域,复合意味着 has-a. 在实现域.复合意味着 is-implemented ...

  3. Javascript函数的基本概念+匿名立即执行函数

    函数声明.函数表达式.匿名函数 函数声明:function fnName () {…};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = f ...

  4. javafx virtual keyboard

    public class EffectTest extends Application { @Override public void start(final Stage stage) { final ...

  5. DG的数据保护模式

    DG的数据保护模式 数据保护膜有三种: – Maximum protection – Maximum availability – Maximum performance Maximum protec ...

  6. 「HAOI2016」字符合并

    「HAOI2016」字符合并 题意: ​ 有一个长度为\(n\)的\(01\)串,你可以每次将相邻的\(k\)个字符合并,得到一个新的字符并获得一定分数.得到的新字符和分数由这\(k\)个字符确定.你 ...

  7. PHP截取字符串长度

    <?php function str_cut($string, $start=0,$length, $dot = '..') {    $strlen = strlen($string);    ...

  8. 小米开源便签Notes-源码研究(0)-整体功能介绍(图文并茂)

    本周对小米开源文件管理器,做了整体的研究,大致弄清了源码的来龙去脉,剩下的就是重点研究几个活动的流程了. 讲解Android应用这种可视化的程序,感觉还是有图比较好,不然功能界面都不清楚,自己不好介绍 ...

  9. wscript shell

    http://blog.csdn.net/songques/article/details/8309569 http://baike.baidu.com/link?url=_P6z73_Ih9R79T ...

  10. UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

    解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <ios ...