![]() |
| "踏上LeetCode解題之路,順手紀錄一下PHP練功的過程囉。這是第五篇~~" |
Merge Two Sorted Lists - 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 $l1
* @param ListNode $l2
* @return ListNode
*/
function mergeTwoLists($l1, $l2) {
if($l1 === NULL) return $l2;
if($l2 === NULL) return $l1;
if($l1->val < $l2->val) {
$l1->next = $this->mergeTwoLists($l1->next, $l2);
return $l1;
} else {
$l2->next = $this->mergeTwoLists($l2->next, $l1);
return $l2;
}
}
}
