链表翻转的迭代和递归解法

迭代:

public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        while (head != null) {
            ListNode current = head.next;
            head.next = pre;
            pre = head;
            head = current;
        }
        return pre;
    }
}

递归:

public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode x = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return x;
    }
}





原文地址:https://www.cnblogs.com/zhayujie/p/7534861.html