File tree Expand file tree Collapse file tree 3 files changed +62
-43
lines changed Expand file tree Collapse file tree 3 files changed +62
-43
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Result: https://leetcode.com/submissions/detail/530298508/
5+ */
6+
7+ require "../vendor/autoload.php " ;
8+
9+ use Leetcode \MergeTwoSortedLists \Solution ;
10+ use Leetcode \MergeTwoSortedLists \ListNode ;
11+ function fillNode ($ array )
12+ {
13+ arsort ($ array );
14+ $ node = null ;
15+ if (sizeof ($ array )) {
16+ foreach ($ array as $ val ) {
17+ $ node = new ListNode ($ val , $ node );
18+ }
19+ return $ node ;
20+ }
21+ }
22+ $ l1 = [-4 , 1 ];
23+ $ l2 = [-7 ,-7 ,-5 ,-3 ,0 ];
24+
25+ $ solution = new Solution ();
26+
27+ $ node1 = fillNode ($ l1 );
28+ $ node2 = fillNode ($ l2 );
29+
30+ $ merged_node = $ solution ->mergeTwoLists ($ node1 , $ node2 );
31+ var_dump ($ merged_node );
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace Leetcode \MergeTwoSortedLists ;
3+ class ListNode
4+ {
5+ public $ val = 0 ;
6+ public $ next = null ;
7+
8+ public function __construct ($ val = 0 , $ next = null )
9+ {
10+ $ this ->val = $ val ;
11+ $ this ->next = $ next ;
12+ }
13+ }
Original file line number Diff line number Diff line change 88 * Difficulty: Easy
99 * Description: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
1010 * Author: Igor Karpov <[email protected] > 11- * Date: 14.01.20
11+ * Date: 29.07.21
1212 * Time: 01:14
1313 * Class Solution
1414 * @package Leetcode\MergeTwoSortedLists
1515 */
1616
17- /**
18- * Definition for a singly-linked list.
19- * class ListNode {
20- * public $val = 0;
21- * public $next = null;
22- * function __construct($val) { $this->val = $val; }
23- * }
24- */
2517class Solution {
2618
2719 /**
2820 * @param ListNode $l1
2921 * @param ListNode $l2
3022 * @return ListNode
3123 */
32- // function mergeTwoLists($l1, $l2) {
33- // print_r($l1);
34- //
35- // if(is_null($l1)) {
36- // return $l2;
37- // }
38- // if(is_null($l2)) {
39- // return $l1;
40- // }
41- // //make head
42- // if($l1->val <= $l2->val) {
43- // $mergedList = new ListNode($l1);
44- // $l1 = $l1->next;
45- // }
46- // else {
47- // $mergedList = new ListNode($l2);
48- // $l2 = $l2->next;
49- //
50- // }
51- // print_r($mergedList);
52- //
53- // while(!is_null($l1) && !is_null($l2)) {
54- // if($l1->val <= $l2->val) {
55- // $mergedList->next = new ListNode($l1);
56- // $l1 = $l1->next;
57- // }
58- // else {
59- // $mergedList->next = new ListNode($l2);
60- // $l2 = $l2->next;
61- // }
62- // }
63- // print_r($mergedList);
64- // }
65- //
24+ function mergeTwoLists ($ l1 , $ l2 ) {
25+
26+ if (is_null ($ l1 )) {
27+ return $ l2 ;
28+ }
29+ if (is_null ($ l2 )) {
30+ return $ l1 ;
31+ }
32+ if ($ l1 ->val < $ l2 ->val ) {
33+ $ l1 ->next = $ this ->mergeTwoLists ($ l1 ->next , $ l2 );
34+ return $ l1 ;
35+ }
36+ else {
37+ $ l2 ->next = $ this ->mergeTwoLists ($ l2 ->next , $ l1 );
38+ return $ l2 ;
39+ }
40+ }
6641}
You can’t perform that action at this time.
0 commit comments