1. /**
  2. * Source : https://oj.leetcode.com/problems/reverse-linked-list-ii/
  3. *
  4. *
  5. * Reverse a linked list from position m to n. Do it in-place and in one-pass.
  6. *
  7. * For example:
  8. * Given 1->2->3->4->5->NULL, m = 2 and n = 4,
  9. *
  10. * return 1->4->3->2->5->NULL.
  11. *
  12. * Note:
  13. * Given m, n satisfy the following condition:
  14. * 1 ≤ m ≤ n ≤ length of list.
  15. */
  16. public class ReverseLinkedList2 {
  17. /**
  18. *
  19. * 反转单向链表指定范围内的元素
  20. *
  21. * 需要考虑第一个元素是否被翻转
  22. *
  23. * @param head
  24. * @param m
  25. * @param n
  26. * @return
  27. */
  28. public Node reverse (Node head, int m, int n) {
  29. if (head == null) {
  30. return head;
  31. }
  32. Node dummy = new Node();
  33. dummy.next = head;
  34. int pos = 1;
  35. Node unreverseListLast = dummy;
  36. Node reverseListLast = null;
  37. Node cur = head;
  38. Node next = null;
  39. Node pre = dummy;
  40. while (cur != null && pos <= n) {
  41. next = cur.next;
  42. if (pos == m) {
  43. unreverseListLast = pre;
  44. reverseListLast = cur;
  45. } else if (pos > m) {
  46. // 在制定范围内,反转
  47. cur.next = pre;
  48. }
  49. pre = cur;
  50. cur = next;
  51. pos++;
  52. }
  53. // 反转完指定范围内的元素,将反转部分和未反转部分连接起来
  54. unreverseListLast.next = pre;
  55. reverseListLast.next = cur;
  56. return dummy.next;
  57. }
  58. private static class Node {
  59. int value;
  60. Node next;
  61. @Override
  62. public String toString() {
  63. return "Node{" +
  64. "value=" + value +
  65. ", next=" + (next == null ? "" : next.value) +
  66. '}';
  67. }
  68. }
  69. private static void print (Node node) {
  70. while (node != null) {
  71. System.out.println(node);
  72. node = node.next;
  73. }
  74. System.out.println();
  75. }
  76. public Node createList (int[] arr) {
  77. if (arr.length == 0) {
  78. return null;
  79. }
  80. Node head = new Node();
  81. head.value = arr[0];
  82. Node pointer = head;
  83. for (int i = 1; i < arr.length; i++) {
  84. Node node = new Node();
  85. node.value = arr[i];
  86. pointer.next = node;
  87. pointer = pointer.next;
  88. }
  89. return head;
  90. }
  91. public static void main(String[] args) {
  92. ReverseLinkedList2 reverseLinkedList2 = new ReverseLinkedList2();
  93. print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 4));
  94. print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 2));
  95. print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 5));
  96. print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 1, 5));
  97. print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{}), 1, 1));
  98. }
  99. }

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

  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 @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...

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

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

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

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

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

  8. lc面试准备:Reverse Linked List II

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

  9. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  10. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

随机推荐

  1. c++ a+b

    #include<iostream> using namespace std; int main() { int a,b,sum; cin>>a>>b; sum=a ...

  2. margin相关属性值

    1.图片与文字对齐问题 图片与文字默认是居底对齐.一般img标签打头的小图标与文字对齐的话,通过 img{margin:0 3px -3px 0;} 这个的东西,能实现效果和兼容性俱佳的对齐效果: d ...

  3. Javascript Read Excel

    本文引用以下路径 https://www.cnblogs.com/liuxianan/p/js-excel.html

  4. 使用Skaffold一键将项目发布到Kubernetes

    当前skaffold版本为v0.4,还未发布正式版本,不建议在生产环境中使用: skaffold用于开发人员快速部署程序到Kubernetes中:skaffold提供了dev.run两种模式:使用sk ...

  5. Visual Studio(VS)秘钥集合

    Visual Studio 2019 Pro :NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y

  6. sql 随机获取数据

    SQL Server: SELECT TOP 10 * FROM T_USER ORDER BY NEWID() ORACLE: SELECT * FROM (SELECT * FROM T_USER ...

  7. aarch64的架构:unrecognized command line option '-mfpu=neon'

    不用添加这个'-mfpu=neon'的编译器选项了,因为这个架构下neon是默认启动的. 参考: https://lists.linaro.org/pipermail/linaro-toolchain ...

  8. LoadRunner(一)——性能测试基础及性能指标概述

    参考学习感谢:<精通软件性能测试与LoadRunner实战> 一.典型的性能测试场景 某个产品要发布了,需要对全市的用户做集中培训.通常在进行培训的时候,老师讲解完成一个业务以后,被培训用 ...

  9. jq 点击复制div里面的内容 如果粘贴到富文本中,会将样式,里面所有的标签,文字一并粘贴进去

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. c++ 获取磁盘句柄

    磁盘的句柄可以用CreateFile函数获得.获得句柄后,就可以配合其他函数对磁盘进行一些操作. int main() { HANDLE hFile = INVALID_HANDLE_VALUE; h ...