题目链接
    image.png
    image.png

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. /**
    12. 每次递归都会得到的数据有
    13. head表示的是当前的节点
    14. 可以有:head head.next
    15. 需要做的是将head.next.next = head;从最后面开始做起
    16. */
    17. class Solution {
    18. public ListNode reverseList(ListNode head) {
    19. if(head == null || head.next == null) return head;
    20. ListNode prev = null,cur;
    21. cur = head;
    22. while(cur != null) {
    23. ListNode tmp = cur.next;
    24. cur.next = prev;
    25. prev = cur;
    26. cur = tmp;
    27. }
    28. return prev;
    29. }
    30. }