给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:
image

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

头插法:还是感觉自己的思路能想得通

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode res = new ListNode();
        while (head != null) {
            ListNode p = head.next;
            head.next = res.next;
            res.next = head;
            head = p;
        }
        return res.next;
    }
}
posted on 2025-07-11 19:59  caoshikui  阅读(7)  评论(0)    收藏  举报