Tuesday, May 10, 2022

[LeetCode] 206. Reverse Linked List

    

"踏上LeetCode解題之路,順手紀錄一下PHP練功的過程囉。這是第十六篇~~"


Reverse Linked List - LeetCode


思考過程

待補上


結果

/**
 * 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) {
        return $this->newReverseList($head, null);
    }
    
    function newReverseList($head, $newHead){
        if($head ==null) return $newHead;
        $next =$head->next;
        $head->next = $newHead;
         return $this->newReverseList($next, $head);
    }
}

Contact Form

Name

Email *

Message *