206. Reverse Linked List

  1. public ListNode reverseList(ListNode head) {
  2. ListNode pre=null;
  3. ListNode cur=head;
  4. while(cur!=null){
  5. ListNode next=cur.next;
  6. cur.next=pre;
  7. pre=cur;
  8. cur=next;
  9. }
  10. return pre;
  11. }
  1. public ListNode reverseList(ListNode head) {
  2. if (head == null || head.next == null) {
  3. return head;
  4. }
  5. ListNode newHead = reverseList(head.next);
  6. head.next.next = head;
  7. head.next = null;
  8. return newHead;
  9. }
  10. 作者:LeetCode-Solution
  11. 链接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/solution/fan-zhuan-lian-biao-by-leetcode-solution-jvs5/