1 Star 1 Fork 0

Allen / data_structure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
11-leetcode2.php 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
Allen 提交于 2020-10-27 00:04 . permit
<?php
/**
* Created by PhpStorm.
* User: allen
* Date: 2019/4/21
* Time: 10:10 PM
*/
class Solution
{
/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
public function addTwoNumbers($l1, $l2)
{
$listNode1 = $l1;
$listNode2 = $l2;
$res = new ListNode(0);
$curr = $res;
$carry = 0;
while ($listNode1 !== null || $listNode2 !== null) {
$total = ($listNode1 == null ? 0 : $listNode1->val) + ($listNode2 == null ? 0 : $listNode2->val) + $carry;
$carry = intval($total / 10);
$curr->next = new ListNode($total % 10);
$curr = $curr->next;
// $curr = new ListNode($total % 10);
if ($listNode1 != null) {
$listNode1 = $listNode1->next;
}
if ($listNode2 != null) {
$listNode2 = $listNode2->next;
}
}
if ($carry > 0) {
$curr->next = new ListNode($carry);
}
return $res->next;
}
}
class ListNode
{
public $val = 0;
public $next = null;
function __construct($val)
{
$this->val = $val;
}
}
$l1 = [2, 4, 3];
$l2 = [5, 6, 4];
$res = (new Solution())->addTwoNumbers(new ListNode($l1[1]),new ListNode($l2[1]));
print_r($res);
PHP
1
https://gitee.com/Alex-e/data_structure.git
git@gitee.com:Alex-e/data_structure.git
Alex-e
data_structure
data_structure
master

搜索帮助