题目链接:牛客NC78反转链表
题目描述:
递归到最底层,回溯时候将节点加到新链表后面
public class Solution {
ListNode ans;
ListNode temp;
public ListNode ReverseList(ListNode head) {
//出口
if(head==null){
ans=new ListNode(0);
temp=ans;
return null;
}
//递归
ReverseList(head.next);
//拼接
temp.next=head;
temp=temp.next;
temp.next=null;
return ans.next;
}
}