示例 1:
输入:[1,2,3,4,5]
返回的结点值为 [5,4,3,2,1]
代码实现
1 | class ListNode |
递推
1 2 3 4 5
$next = [2, 3, 4, 5];
$currentNode->next = null;
$reverseList = 1;
$currentNode = $next = [2, 3, 4, 5]
$next = [3, 4, 5];
$currentNode->next = [1];
$reverseList = [2, 1]
$currentNode = $next = [3, 4, 5]
$next = [4, 5];
$currentNode->next = [2, 1];
$reverseList = [3, 2, 1]
$currentNode = $next = [4, 5]
$next = [5];
$currentNode->next = [3, 2, 1];
$reverseList = [4, 3, 2, 1]
$currentNode = $next = [5]
$reverseList = [5, 4, 3, 2, 1]
运行结果
1 | 5 |