两种做法
第一种,递归。递归到尾节点,回溯时反转
第二种,定义三个指针,向尾节点遍历的同时反转
0ms 100%
class Solution {
ListNode ans;
void build(ListNode t){
if(t.next == null){
ans = t;
return;
}
build(t.next);
t.next.next = t;
t.next = null;
}
public ListNode reverseList(ListNode head) {
if(head == null)
return null;
build(head);
return ans;
}
}