1. package top.ssyuan.medium;
    2. import java.util.Stack;
    3. import top.ssyuan.Util;
    4. import top.ssyuan.easy.Lesson206;
    5. /**
    6. * 92. Reverse Linked List II
    7. * Medium
    8. * <p>
    9. * Given the head of a singly linked list and two integers left and right where left <= right,
    10. * reverse the nodes of the list from position left to position right, and return the reversed list.
    11. * <p>
    12. * <p>
    13. * Example 1:
    14. * <p>
    15. * <p>
    16. * Input: head = [1,2,3,4,5], left = 2, right = 4
    17. * Output: [1,4,3,2,5]
    18. * Example 2:
    19. * <p>
    20. * Input: head = [5], left = 1, right = 1
    21. * Output: [5]
    22. * <p>
    23. * <p>
    24. * Constraints:
    25. * <p>
    26. * The number of nodes in the list is n.
    27. * 1 <= n <= 500
    28. * -500 <= Node.val <= 500
    29. * 1 <= left <= right <= n
    30. */
    31. public class Lesson92 {
    32. public static void main(String[] args) {
    33. Lesson92 lesson = new Lesson92();
    34. ListNode head = lesson.genLinkList(10);
    35. lesson.printLink(head);
    36. head = new Solution1().reverseBetween(head, 1, 3);
    37. Util.println("");
    38. lesson.printLink(head);
    39. }
    40. private void printLink(ListNode head) {
    41. ListNode node = head;
    42. while (node != null) {
    43. Util.print(node.val + "->");
    44. node = node.next;
    45. }
    46. }
    47. private ListNode genLinkList(int count) {
    48. if (count < 1) return null;
    49. ListNode head = new ListNode((int) (Math.random() * 100));
    50. ListNode curNode = head;
    51. for (int i = 1; i < count; i++) {
    52. curNode.next = new ListNode((int) (Math.random() * 100));
    53. curNode = curNode.next;
    54. }
    55. return head;
    56. }
    57. //Definition for singly-linked list.
    58. private class ListNode {
    59. int val;
    60. ListNode next;
    61. ListNode() {
    62. }
    63. ListNode(int val) {
    64. this.val = val;
    65. }
    66. ListNode(int val, ListNode next) {
    67. this.val = val;
    68. this.next = next;
    69. }
    70. }
    71. private static class Solution1 {
    72. public ListNode reverseBetween(ListNode head, int left, int right) {
    73. if (head == null || left > right) {
    74. return null;
    75. }
    76. Stack<ListNode> stack = new Stack<>();
    77. ListNode node = head;
    78. ListNode pre = head;
    79. ListNode next = null;
    80. int index = 0;
    81. while (node != null) {
    82. if (index < left - 1) {
    83. pre = node;
    84. }
    85. if (index >= left - 1 && index < right) {
    86. stack.push(node);
    87. }
    88. if (index >= right) {
    89. next = node;
    90. break;
    91. }
    92. node = node.next;
    93. index++;
    94. }
    95. ListNode head2 = stack.pop();
    96. node = head2;
    97. while (!stack.empty()) {
    98. node.next = stack.pop();
    99. node = node.next;
    100. }
    101. pre.next = head2;
    102. if (node != null) {
    103. node.next = next;
    104. }
    105. return left == 1 ? head2 : head;
    106. }
    107. }
    108. private static class Solution2 {
    109. public ListNode reverseBetween(ListNode head, int left, int right) {
    110. if (head == null || left >= right) {
    111. return null;
    112. }
    113. ListNode node = head;
    114. ListNode pre = null;
    115. ListNode tmp = node;
    116. while (node != null) {
    117. tmp = node;
    118. node = node.next;
    119. tmp.next = pre;
    120. pre = tmp;
    121. }
    122. return tmp;
    123. }
    124. }
    125. }