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