206. 反转链表

链接 (opens new window)

20240221213842_image.png

class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function reverseList($head) {
        if (!$head->next) {
            return $head;
        }
        $tail = $this->reverseList($head->next);
        printf("tail:%d,head:%d,head->next:%d\n", $tail->val,$head->val, $head->next->val);
        $head->next->next = $head;
        $head->next = null;
        return $tail;
    }
}

copy success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18