题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
//通过递归做:将链表看成特殊二叉树
//截至条件:当为叶子节点或者是节点为空截至
if(head==null||head.next==null){
return head;
}
//先将head。next后面的全部逆置,返回头节点
ListNode newhead = ReverseList(head.next);
//将当前节点添加到逆置后的链表后面,就是改变这两个节点的指向
head.next.next = head;
head.next = null;
return newhead;
}
}

查看27道真题和解析