迭代:
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;
}
}