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.

Analysis: 这道题还是想了我蛮久,先是题意理解错了,以为只是m和n这两个位置对调,结果发现其实是m到n之间的所有元素都需要调换。一时之间没有想到怎样做reverse比较好,参考了一下网上的思路,发现这样做比较好:还是要用Runner Technique,还是要用Dummy Node;两个指针: npointer指到n的位置,mpointer指到m的前一位;每一次把mpointer后一位的元素放到npointer的后一位:mpointer.next.next = npointer.next;直到mpointer.next = npointer为止(m与n重合)

Original linked list:       1->2->3->4->5->6->7; m = 3, n =6

Step1:        1->2->4->5->6->3->7

Step2:      1->2->5->6->4->3->7

......

Result:      1->2->6->5->4->3->7

Note that pointer m is switching to right one by one in each step, but pointer n remains no change.

再次体现了在链表题中使用Dummy Node, 并且对当前node.next进行操作的好处。

Notice: 像这种链表删除插入操作,在删除插入之前,最好先拷贝一下被删除节点的下一个节点,以及插入位置的下一个节点,把它们存在一个变量ListNode store里面,直接去访问这个变量,而不要用类似mpointer.next的方式存着他们,因为这样受制于mpointer,mpointer一旦变化,就找不到这些删除/插入节点的下一个节点了。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode reverseBetween(ListNode head, int m, int n) {
14         ListNode prev = new ListNode(-1);
15         prev.next = head;
16         ListNode mpointer = prev; //point to m-1 position
17         ListNode npointer = prev; //point to n position
18         while (m > 1) {
19             mpointer = mpointer.next;
20             m--;
21         }
22         while (n > 0) {
23             npointer = npointer.next;
24             n--;
25         }
26         while (mpointer.next != npointer) {
27             ListNode mnext = mpointer.next.next;
28             ListNode nnext = npointer.next;
29             mpointer.next.next = nnext;
30             npointer.next = mpointer.next;
31             mpointer.next = mnext;
32         }
33         return prev.next;
34     }
35 }

第二遍做法:

 1 public class Solution {
 2     public ListNode reverseBetween(ListNode head, int m, int n) {
 3         if (head == null || m > n) return head;
 4         ListNode dummy = new ListNode(-1);
 5         dummy.next = head;
 6         ListNode walker = dummy;
 7         ListNode runner = dummy;
 8         while (m > 1) {
 9             walker = walker.next;
10             m--;
11         }
12         while (n > 0) {
13             runner = runner.next;
14             n--;
15         }
16         while (walker.next != runner) {
17             ListNode cur = walker.next;
18             ListNode next = cur.next;
19             cur.next = runner.next;
20             runner.next = cur;
21             walker.next = next;
22         }
23         return dummy.next;
24     }
25 }

Leetcode: Reverse Linked List II相关推荐

  1. LeetCode | Reverse Linked List II

    这道链表反转题也是搞的我焦头烂额,好久没有写链表了,注意记忆这些 Reverse Linked List II QuestionEditorial Solution My Submissions To ...

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

  3. leetcode : Reverse Linked List II [two pointers]

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

  4. LeetCode Reverse Linked List II

    链表局部反转问题 代码如下: class Solution {public ListNode reverseBetween(ListNode head, int m, int n) {if (m == ...

  5. LeetCode Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

  6. 【??链表】LeetCode 92. Reverse Linked List II

    LeetCode 92. Reverse Linked List II Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4306611.html ...

  7. LeetCode 92. Reverse Linked List II

    92. Reverse Linked List II Reverse a linked list from position m to n. Do it in one-pass. 将位置m的链接列表反 ...

  8. (LeetCode 92)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. Reverse Linked List II leetcode java

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

最新文章

  1. Android视图重绘,使用invalidate还是requestLayout
  2. Numpy:数组合矢量计算
  3. 2017-06-18 前端日报
  4. 51nod1667-概率好题【容斥,组合数学】
  5. Android 之父裁员 30%:开发者如何避免“被离职”?
  6. spl_autoload_register 注册自己的自动加载函数(__autoload())
  7. Eureka Server集群同步
  8. iPad mini 6外形巨变:升级全面屏 还要砍掉Home键
  9. 弘辽科技:淘宝流量下滑了怎么办?淘宝流量下滑应对方法
  10. 手机APP测试——Perfecto
  11. 车牌识别,车辆检测,车牌检测和识别,与车相关的点点滴滴
  12. 排列组合相关笔试面试题(C++)
  13. 乐吾乐2D可视化为智慧电力赋能(二)
  14. FANUC机器人PNS自动运行方式详细介绍
  15. 结构建模设计——Solidworks软件之装配体操作基本总结二(装配体内编辑零件、新建零件、标准配合操作)
  16. 云课堂智慧职教答案python_智慧职教云课堂Python程序设计答案
  17. python 列表操作之合并
  18. 数据中台产品设计方法论
  19. sci hub论文下载方法及脚本插件安装
  20. JS+CSS实现一个底部渐变波浪效果

热门文章

  1. Sqlserver中一直在用又经常被忽略的知识点一
  2. ios开发学习--cocos2d(cocos2d)效果源码分享--系列教程
  3. 中国电信制定物联网策略:规模市场自主经营 长尾市场集成
  4. javascript 内部函数的定义及调用
  5. C语言 · 求矩阵各个元素的和
  6. Minor【 PHP框架】3.路由、控制器、视图
  7. linux下crontab实现定时服务详解
  8. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)
  9. js控制select大全
  10. IBM确定公司未来存储技术发展方向