https://leetcode-cn.com/problems/reverse-linked-list/
public ListNode reverseList(ListNode head) {// 迭代// 想要反转,重点是将指向改变。ListNode pre = null;ListNode cur = head;// 最终,当前指向为null时,结束。// 不过,最终返回的是cur的前一指向prewhile (cur != null) {// 首先要记录下cur当前的下一指向,用于将cur稍后指向这一指向// 这样才能一直往后迭代ListNode temp = cur.next;// 将当前的next指向前面一个,这就完成了反转cur.next = pre;// 但是,结束反转。要加当前cur变为pre,用于下一轮的指向pre = cur;// 同理,要将当前cur变为刚刚存储的原来的下一个指向,这样就实现了往后迭代cur = temp;}return pre;// 递龟if (head == null || head.next == null) {return head;}ListNode newHead = reverseList(head.next);// 产生倒置转化在这一步,head.next是原来的下一指向// 将它的下一指向,指向现在的节点,那是不是就完成了倒置?head.next.next = head;// 此外!我们没有操作当前的下一指向,也就是说// head.next还是指向原来的next,这就会产生环head.next = null;return newHead;}
