题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
private ListNode newHead;
public ListNode ReverseList(ListNode head) {
ReverseList(null, head);
return newHead;
}
//翻转指定链表
private void ReverseList(ListNode xFrom, ListNode x) {
if (x == null) {
//如果x为空,则x的前一个结点,即xFrom为新链表的头部
newHead = xFrom;
return;
}
//把大问题转换为小问题
ReverseList(x, x.next);
x.next = xFrom;
}
}
