Skip to content

Commit 05d1c5e

Browse files
authored
Added task 2
1 parent 9e4322b commit 05d1c5e

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"autoload-dev": {
2828
"psr-4": {
29-
"leetcode\\tests\\": "src/test/php/"
29+
"leetcode\\": "src/test/php/"
3030
}
3131
},
3232
"extra": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace leetcode\com_github_leetcode;
4+
5+
class ListNode {
6+
public $val;
7+
public $next;
8+
9+
public function __construct($val = 0, $next = null) {
10+
$this->val = $val;
11+
$this->next = $next;
12+
}
13+
14+
public function toString() {
15+
$result = (string) $this->val;
16+
17+
if ($this->next !== null) {
18+
$current = $this->next;
19+
20+
while ($current->next !== null) {
21+
$result .= ', ' . $current->val;
22+
$current = $current->next;
23+
}
24+
25+
$result .= ', ' . $current->val;
26+
}
27+
28+
return $result;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0002_add_two_numbers;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
6+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
7+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2023_11_29_Time_13_ms_(73.83%)_Space_19.2_MB_(38.29%)
8+
9+
use leetcode\com_github_leetcode\ListNode;
10+
11+
/**
12+
* Definition for a singly-linked list.
13+
* class ListNode {
14+
* public $val = 0;
15+
* public $next = null;
16+
* function __construct($val = 0, $next = null) {
17+
* $this->val = $val;
18+
* $this->next = $next;
19+
* }
20+
* }
21+
*/
22+
class Solution {
23+
/**
24+
* @param ListNode $l1
25+
* @param ListNode $l2
26+
* @return ListNode
27+
*/
28+
function addTwoNumbers($l1, $l2) {
29+
$dummyHead = new ListNode(0);
30+
$p = $l1;
31+
$q = $l2;
32+
$curr = $dummyHead;
33+
$carry = 0;
34+
35+
while ($p !== null || $q !== null) {
36+
$x = ($p !== null) ? $p->val : 0;
37+
$y = ($q !== null) ? $q->val : 0;
38+
$sum = $carry + $x + $y;
39+
$carry = (int)($sum / 10);
40+
41+
$curr->next = new ListNode($sum % 10);
42+
$curr = $curr->next;
43+
44+
if ($p !== null) {
45+
$p = $p->next;
46+
}
47+
if ($q !== null) {
48+
$q = $q->next;
49+
}
50+
}
51+
52+
if ($carry > 0) {
53+
$curr->next = new ListNode($carry);
54+
}
55+
56+
return $dummyHead->next;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace leetcode\com_github_leetcode;
4+
5+
use leetcode\com_github_leetcode\ListNode;
6+
7+
class LinkedListUtils {
8+
public static function constructLinkedList($nums) {
9+
if ($nums === null || count($nums) === 0) {
10+
return null;
11+
}
12+
$pre = new ListNode(-1);
13+
$head = new ListNode($nums[0]);
14+
$pre->next = $head;
15+
for ($i = 1; $i < count($nums); $i++) {
16+
$head->next = new ListNode($nums[$i]);
17+
$head = $head->next;
18+
}
19+
return $pre->next;
20+
}
21+
22+
public static function createSinglyLinkedList($listValues) {
23+
if ($listValues === null || count($listValues) === 0) {
24+
throw new InvalidArgumentException(
25+
"Please pass in a valid listValues to create a singly linked list."
26+
);
27+
}
28+
$head = new ListNode($listValues[0]);
29+
$tmp = $head;
30+
for ($i = 1; $i < count($listValues); $i++) {
31+
$next = new ListNode($listValues[$i]);
32+
$tmp->next = $next;
33+
$tmp = $tmp->next;
34+
}
35+
return $head;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0002_add_two_numbers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use leetcode\com_github_leetcode\ListNode;
7+
use leetcode\com_github_leetcode\LinkedListUtils;
8+
9+
class SolutionTest extends TestCase {
10+
public function testAddTwoNumbers() {
11+
$listNode1 = LinkedListUtils::constructLinkedList([2, 4, 3]);
12+
$listNode2 = LinkedListUtils::constructLinkedList([5, 6, 4]);
13+
$this->assertThat(
14+
(new Solution())->addTwoNumbers($listNode1, $listNode2)->toString(),
15+
$this->equalTo("7, 0, 8")
16+
);
17+
}
18+
19+
public function testAddTwoNumbers2() {
20+
$this->assertThat(
21+
(new Solution())->addTwoNumbers(new ListNode(0), new ListNode(0))->toString(),
22+
$this->equalTo("0")
23+
);
24+
}
25+
26+
public function testAddTwoNumbers3() {
27+
$listNode1 = LinkedListUtils::constructLinkedList([9, 9, 9, 9, 9, 9, 9]);
28+
$listNode2 = LinkedListUtils::constructLinkedList([9, 9, 9, 9]);
29+
$this->assertThat(
30+
(new Solution())->addTwoNumbers($listNode1, $listNode2)->toString(),
31+
$this->equalTo("8, 9, 9, 9, 0, 0, 0, 1")
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)