1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| // @Title: 反转链表 (Reverse Linked List)
// @Author: 15816537946@163.com
// @Date: 2022-03-21 14:42:41
// @Runtime: 8 ms
// @Memory: 20.8 MB
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $head
* @return ListNode
*/
function reverseList($head)
{
if ($head->next === null) return $head;
$last = $this->reverseList($head->next);
$head->next->next = $head;
$head->next = null;
return $last;
}
}
|