P9. 反转单链表

https://algocasts.io/episodes/QrWZbW5w

@ted_woo

代码是没问题的。while 循环里有 cur.next = pre; 而 pre 初始就是 null,自然也就是把原链表中 head 的 next 设置为 null 了。附上原代码:

  // Time: O(n), Space: O(1)
  public ListNode reverseList(ListNode head) {
    ListNode cur = head, pre = null;
    while (cur != null) {
      ListNode next = cur.next;
      cur.next = pre;
      pre = cur;
      cur = next;
    }
    return pre;
  }

哦,对的,我看得不够仔细