206. 反转链表

image.png

题解

迭代法

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户 内存消耗:38.3 MB, 在所有 Java 提交中击败了43.94% 的用户

  1. class Solution {
  2. public ListNode reverseList(ListNode head) {
  3. ListNode pre = null, next = null;
  4. while (head != null) {
  5. next = head.next;
  6. head.next = pre;
  7. pre = head;
  8. head = next;
  9. }
  10. return pre;
  11. }
  12. }