问题

解法
1. 递归 O(N)


class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode nextNode=head.next;
ListNode newHead=reverseList(nextNode);
head.next=null;
nextNode.next=head;
return newHead;
}
}
class Solution {
public ListNode reverseList(ListNode head, ListNode prev) {
if(head==null) return prev;
ListNode next = head.next;
head.next = prev;
return reverseList(next,head)
}
}
2. 非递归:前插法 O(N)

class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode front = new ListNode(0);
while(head!=null){
ListNode temp = head.next;
head.next=front.next;
front.next=head;
head=temp;
}
return front.next;
}
}