问题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7f1cbc5a4f51d30c2f04b09c8b0e7172.png)
解法
1. 递归 O(N)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c74bc5580508b7ebb23890f7e305175f.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2307a3f4a75c5c661613841d3e170e14.png)
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)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/597fcb9759306244d3a29e721d1c2c74.png)
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;
}
}