题解 | #反转链表#
反转链表
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) {
        //记录上一个节点
        ListNode preNode = null;
        //翻转后的链表
        ListNode reverseNode = null;
        while(head != null) {
            ListNode next = head.next; 
            //反转链表初始化节点是head
            reverseNode = head;
            //指向上一个节点,默认上一个第一次是null(反转后的最后一个)
            reverseNode.next = preNode;
            //当前节点编程上一个节点
            preNode = head;
            //继续向后遍历head
            head = next;
        }
        return reverseNode;
    }
}
#链表反转#
查看17道真题和解析