代码拉取完成,页面将自动刷新
<?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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。