反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例

[链表] 系列:反转链表 - 图1

解题

方法一:迭代

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

方法二:递归