Add Two Numbers 的php解法

码农天地 -
Add Two Numbers 的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 $l1
     * @param ListNode $l2
     * @return ListNode
     */
    function addTwoNumbers($l1, $l2) {
        $result=(is_null($l1)?0:$l1->val)+(is_null($l2)?0:$l2->val);
        if($result>=10){
            $result-=10;
            if(is_null($l1->next)){
                $l1->next=new ListNode;
            }
            $l1->next->val+=1;
        }
        $node=new ListNode($result);
        if(is_null($l1->next) && is_null($l2->next)){
            return $node;
        }
        $node->next=$this->addTwoNumbers($l1->next,$l2->next);
        return $node;
    }
}

个人博客:https://www.gwlin.com/posts/5717

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

php介绍

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。

Tags 标签

php算法leetcode

扩展阅读

加个好友,技术交流

1628738909466805.jpg