流程图:

image.png

代码示例

单向列表反转代码
  1. public static class Node{
  2. public int value;
  3. public Node next;
  4. public Node(int value){
  5. this.value = value;
  6. }
  7. }
  8. public static Node reverseNode(Node head){
  9. Node next = null;
  10. Node prev = null;
  11. while (head != null){
  12. next = head.next;
  13. head.next= prev;
  14. prev = head;
  15. head = next;
  16. }
  17. return prev;
  18. }

单向链表反转包含测试的代码
  1. package com.ss.class02;
  2. import java.util.Random;
  3. /**
  4. * <p>
  5. * 反转列表
  6. * </P>
  7. *
  8. * @author: zhangss
  9. * @since: 2021-01-07
  10. **/
  11. public class ReverseList {
  12. /**
  13. * 单向链表
  14. */
  15. public static class Node{
  16. public int value;
  17. public Node next;
  18. public Node(int value){
  19. this.value = value;
  20. }
  21. }
  22. /**
  23. * 双向链表
  24. */
  25. class DoubleNode{
  26. public int value;
  27. public DoubleNode prevNode;
  28. public DoubleNode nextNode;
  29. public DoubleNode(int value){
  30. this.value = value;
  31. }
  32. }
  33. /**
  34. * 反转单向链表,反转以后返回新的头部
  35. * @param head
  36. * @return
  37. */
  38. public static Node reverseNode(Node head){
  39. Node next = null;
  40. Node prev = null;
  41. while (head != null){
  42. next = head.next;
  43. head.next= prev;
  44. prev = head;
  45. head = next;
  46. }
  47. return prev;
  48. }
  49. /**
  50. * 生成单向链表
  51. * @return
  52. */
  53. public static Node generateNode(){
  54. Random random = new Random();
  55. Node head = new Node(random.nextInt(1000));
  56. Node tail = head;
  57. int length = random.nextInt(100);
  58. for (int i = 0; i < length; i++) {
  59. tail.next = new Node(random.nextInt(100));
  60. tail = tail.next;
  61. }
  62. return head;
  63. }
  64. /**
  65. * 反转双向链表
  66. * @param doubleNode
  67. * @return
  68. */
  69. public static DoubleNode reverseDoubleNode(DoubleNode doubleNode) {
  70. return null;
  71. }
  72. /**
  73. * 测试单向链表反转
  74. * @param args
  75. */
  76. public static void main(String[] args) {
  77. for(int i=0;i< 50_0000;i++){
  78. Node head = generateNode();
  79. // 反转两次和原来的单向链表一致,说明反转方法是正确的
  80. Node newHead = reverseNode(reverseNode(head));
  81. if(!nodeEquals(head, newHead)){
  82. System.out.println("reverse failed.");
  83. }
  84. }
  85. }
  86. /**
  87. * 判断两个单向链表是否相等
  88. * 转成string进行判断
  89. * @param head1
  90. * @param head2
  91. * @return
  92. */
  93. private static boolean nodeEquals(Node head1, Node head2){
  94. String head1Val = nodeToString(head1);
  95. String head2Val = nodeToString(head2);
  96. if (head1Val.equals(head2Val)) {
  97. return true;
  98. }else {
  99. return false;
  100. }
  101. }
  102. /**
  103. * 单向链表转string
  104. * @param head
  105. * @return
  106. */
  107. private static String nodeToString(Node head){
  108. String value = "";
  109. while (head != null) {
  110. value = value + head.value;
  111. head = head.next;
  112. }
  113. return value;
  114. }
  115. public static void print(Node head){
  116. Node next = head;
  117. while (next != null) {
  118. System.out.println(next.value);
  119. next = next.next;
  120. }
  121. }
  122. }