题目描述

题解

反转链表的题目都可以按照模板进行一些小小的修改,首先看一下模板

对模板修改后,得到结果

public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;
    ListNode next = null;

    while(curr != null){
        next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }

    return prev;
}