题解 | #反转链表# 递归解决反转链表
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public ListNode ReverseList (ListNode head) {
return reverseList(null,head);
}
//递归需要传入两个内容
private ListNode reverseList (ListNode pre, ListNode cur) {
if(cur==null){
return pre;
}
ListNode next = cur.next;
cur.next = pre;
return reverseList(cur,next);
}
}
#反转链表java#