leetcode 92. Reverse Linked List II

悠悠 2023-07-17 15:46 23阅读 0赞

https://leetcode.com/problems/reverse-linked-list-ii/

将链表从m到n的位置反转,链表的位置从1记起,1<=m<=n<=链表长度

一、问题描述

测试用例:

  1. Example:
  2. Input: 1->2->3->4->5->NULL, m = 2, n = 4
  3. Output: 1->4->3->2->5->NULL

要求只进行一遍遍历。

二、代码实现

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode reverseBetween3(ListNode head, int m, int n) {
  11. if (head == null || head.next == null || m == n) {
  12. return head;
  13. }
  14. //添加一个伪头节点
  15. ListNode newHead = new ListNode(-1);
  16. newHead.next = head;
  17. ListNode pre1 = newHead, pre2 = newHead;
  18. // int pos = 1;
  19. // while (pos < m) {
  20. // pre1 = pre1.next;
  21. // pre2 = pre2.next;
  22. // pos ++;
  23. // }
  24. // while (pos < n+1) {
  25. // pre2 = pre2.next;
  26. // pos ++;
  27. // }
  28. for (int i=0; i<m-1; i++) {
  29. pre1 = pre1.next;
  30. }
  31. for (int i=0; i<n; i++) {
  32. pre2 = pre2.next;
  33. }
  34. System.out.println(pre1.val +" "+ pre2.val);
  35. ListNode temp = reverseList(pre1.next, pre2.next);
  36. pre1.next = temp;
  37. return newHead.next;
  38. }
  39. public ListNode reverseList(ListNode head, ListNode bound) {
  40. if (head == null || head.next == null) {
  41. return head;
  42. }
  43. ListNode cur = head, newBound = bound;
  44. while (cur.next != bound) {
  45. //System.out.println(cur.val+" "+bound.val); //bound可能为null,bound!=null时才可以打印
  46. ListNode next = cur.next;
  47. cur.next = newBound;
  48. newBound = cur;
  49. cur = next;
  50. }
  51. cur.next = newBound;
  52. return cur;
  53. }
  54. public ListNode reverseBetween2(ListNode head, int m, int n) {
  55. if (head == null || head.next == null || m == n) {
  56. return head;
  57. }
  58. //添加一个伪头节点
  59. ListNode newHead = new ListNode(-1);
  60. newHead.next = head;
  61. ListNode pre1 = newHead, pre2 = newHead;
  62. int pos = 1;
  63. while (pos < m) {
  64. pre1 = pre1.next;
  65. pre2 = pre2.next;
  66. pos ++;
  67. }
  68. while (pos < n+1) {
  69. pre2 = pre2.next;
  70. pos ++;
  71. }
  72. System.out.println(pre1.val +" "+ pre2.val);
  73. ListNode temp = pre2;
  74. pre2 = pre2.next;
  75. temp.next = null;
  76. temp = reverseList(pre1.next);
  77. pre1.next = temp;
  78. if (pre2 == null) {
  79. return newHead.next;
  80. }
  81. ListNode cur = temp;
  82. while (cur.next != null) {
  83. cur = cur.next;
  84. }
  85. cur.next = pre2;
  86. return newHead.next;
  87. }
  88. public ListNode reverseList(ListNode head) {
  89. if (head == null || head.next == null) {
  90. return head;
  91. }
  92. ListNode pre = null, cur = head;
  93. while (cur != null) {
  94. ListNode next = cur.next;
  95. cur.next = pre;
  96. pre = cur;
  97. cur = next;
  98. }
  99. return pre;
  100. }
  101. //[3, 5] 1 2 输出[3] 期待[5, 3]
  102. public ListNode reverseBetween_error(ListNode head, int m, int n) {
  103. //[5] 1 1 NullPointerException
  104. if (head == null || head.next == null) {
  105. return head;
  106. }
  107. //[3,5] 2 2 输出[3] 期待[3, 5]
  108. if (m == n) {
  109. return head;
  110. }
  111. ListNode pre = head, next = head;
  112. int count = 1;
  113. while (count < m-1) {
  114. pre = pre.next;
  115. count++;
  116. }
  117. count = 1;
  118. while (count <= n) {
  119. next = next.next;
  120. count++;
  121. }
  122. //System.out.println(pre.val +" "+ next.val);
  123. ListNode cur = pre.next;
  124. while (cur != next && cur.next != null) {
  125. ListNode temp = cur.next;
  126. cur.next = next;
  127. next = cur;
  128. cur = temp;
  129. }
  130. pre.next = next;
  131. return head;
  132. }
  133. public ListNode reverseBetween1(ListNode head, int m, int n) {
  134. if (head == null || m == n) {
  135. return head;
  136. }
  137. if (m == 1) {
  138. ListNode tempHead = null;
  139. ListNode pre = null;
  140. int flag = 1;
  141. int count = 0;
  142. while (head != null) {
  143. ListNode temp = head;
  144. head = head.next;
  145. count++;
  146. temp.next = tempHead;
  147. tempHead = temp;
  148. if (flag == 1) {
  149. pre = tempHead;
  150. flag = -flag;
  151. }
  152. if (count == (n-m+1)) {
  153. //null pointer
  154. //head = head.next;
  155. break;
  156. }
  157. }
  158. pre.next = head;
  159. return tempHead;
  160. }
  161. int count = 1;
  162. ListNode cur = head;
  163. ListNode tail = null;
  164. while (cur != null) {
  165. if (count == m - 1) {
  166. tail = cur;
  167. break;
  168. }
  169. cur = cur.next;
  170. System.out.println(cur.val+" "+count);
  171. count++;
  172. }
  173. //cur is the m-1
  174. System.out.println(tail.val);
  175. cur = cur.next;
  176. ListNode tail2 = cur, head2 = cur;
  177. System.out.println(head2.val+" "+tail2.val);
  178. cur = cur.next;
  179. count = 2;
  180. System.out.println("======================");
  181. System.out.println(cur.val);
  182. while (cur != null) {
  183. ListNode temp = cur;
  184. cur = cur.next;
  185. temp.next = head2;
  186. tail.next = temp;
  187. head2 = temp;
  188. count++;
  189. if (count == (n-m+1 +1)) {
  190. break;
  191. }
  192. System.out.println("hhhhhhhhhhhhh");
  193. }
  194. //null pointer
  195. //System.out.println(cur.val);
  196. tail2.next = cur;
  197. return head;
  198. }
  199. }

参考:

https://leetcode.com/problems/reverse-linked-list-ii/discuss/30667/Easy-understanding-java-solution(遍历一遍)

https://leetcode.com/problems/reverse-linked-list-ii/discuss/30672/Python-one-pass-iterative-solution(方法同上)

https://leetcode.com/problems/reverse-linked-list-ii/discuss/30666/Simple-Java-solution-with-clear-explanation(方法同上)

https://leetcode.com/problems/reverse-linked-list-ii/discuss/30668/C%2B%2B-simple

发表评论

表情:
评论列表 (有 0 条评论,23人围观)

还没有评论,来说两句吧...

相关阅读