<?php /** * 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) { $cur = $head; $prev = null; while ($cur) { $nextNode = $cur->next; $cur->next = $prev; $prev = $cur; $cur = $nextNode; } return $prev; } }
leetcode刷题----反转链表
最新推荐文章于 2024-10-14 17:32:24 发布