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->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

题目意思:

翻转给定区间[m,n]的链表。

第一个节点m=1。

1<=m<=n<=length。

解题思路:

就我目前学习到的,有用指向指针的指针的,有插入,有逆转的。但是所有方法的核心,都其实是一个算法,就是利用3个指针修改区间内的节点的next值,且要保证已修改的链表是可以被找到的,即可以连入原链表中。

假设有一个链表有1,2,3,4,5,6,6个节点。m=2,n=5。

我们添加一个dummy节点,方便操作第一个节点。

首先让pre指向第m个节点前面那个,cur指向第m个节点,p1指向m的下一个节点,因为p1有可能是NULL,所以当p1不是NULL的时候,p2指向p1的下一个节点。

上图画出了这几个指针指向情况的开始状态和我们希望的终止状态。

在最终状态下,再通过其中方框中的两步就可以我们需要的链表了。

而cur,p1,p2三个指针在区间内向前移动并且将p1的next指向cur的过程则包含在一个for循环内部。如下:

代码如下:

  1. class Solution {
  2. public:
  3. ListNode *reverseBetween(ListNode *head, int m, int n) {
  4. ListNode *dummy = new ListNode();
  5. dummy->next = head;
  6.  
  7. ListNode *pre = dummy, *cur = head;
  8. for(int i = ; i < m; i++){
  9. pre = cur;
  10. cur = cur->next;
  11. }
  12.  
  13. ListNode *p1,*p2;
  14. if(cur)
  15. p1 = cur->next;
  16. if(p1)
  17. p2 = p1->next;
  18. for(int j = m; j < n; j++){
  19. p1->next = cur;
  20. cur = p1;
  21. p1 = p2;
  22. if(p2) p2 = p2->next;
  23. }
  24. pre->next->next = p1;
  25. pre->next = cur;
  26.  
  27. return dummy->next;
  28. }
  29. };

【LeetCode练习题】Reverse Linked List II的更多相关文章

  1. 【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-pass. F ...

  2. leetcode -day30 Reverse Linked List II

    1.  Reverse Linked List II  Reverse a linked list from position m to n. Do it in-place and in one- ...

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

  4. Java for LeetCode 092 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-> ...

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

  6. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  7. leetcode 92 Reverse Linked List II ----- java

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

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

  9. LeetCode 92. Reverse Linked List II倒置链表2 C++

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

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

随机推荐

  1. RBF径向基神经网络——乳腺癌医学诊断建模

    案例描述 近年来疾病早期诊断越来越受到医学专家的重视,从而产生了各种疾病诊断的新方法.乳癌最早的表现是患乳出现单发的.无痛性并呈进行性生长的小肿块.肿块位于外上象限最多见,其次是乳头.乳晕区和内上象限 ...

  2. PKU 3667 Hotel(线段树)

    Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  3. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...

  4. (step6.1.3)hdu 1875(畅通工程再续——最小生成树)

    题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...

  5. 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台

    ---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...

  6. 关于 jsp:include 传参的用法

    引用模版页面的代码,如下: <jsp:include page="/WEB-INF/template/nav_template.jsp">     <jsp:pa ...

  7. android onIntent 是什么东西

    在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...

  8. Android服务之AIDL

    在android开发过程中,为了让其他的应用程序,也可以访问本应用程序的服务,android系统采用远程过程调用来实现.android通过接口来公开定义的服务.我们将能够夸进程访问的服务成为AIDL服 ...

  9. Xcode5 配置 github

    首先,要在github上,进行如下的操作: 1. github 官网 https://github.com  注册github账号. 2. 创建一个repository,命名为项目的名称,如 Gith ...

  10. PHP基础之 define() 函数

    定义和用法 define() 函数定义一个常量. 常量类似变量,不同之处在于: 在设定以后,常量的值无法更改 常量名不需要开头的美元符号 ($) 作用域不影响对常量的访问 常量值只能是字符串或数字 语 ...