24. 反转链表
思路:三指针
pre、cur、curNext
- 初始化:
- 第一个节点指向null
- cur.next指向pre
- 挪动+cur.next指向pre
- 直到curNext是空+还须要一次cur.next指向pre,返回cur
- 或者直到cur是空,返回pre
操作:
思路:递归
- 返回条件:head==null||head.next==null
- 递归体:
cur的下一个节点的next指针指向cur
cur的next指针指向null
操作:
public ListNode reverseList(ListNode head) { if (head==null||head.next==null) return head; ListNode newhead = reverseList(head.next); head.next.next = head; head.next = null; return newhead; }