-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.php
More file actions
54 lines (46 loc) · 1.07 KB
/
21.php
File metadata and controls
54 lines (46 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution {
/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function mergeTwoLists($l1, $l2)
{
if (null == $l1) return $l2;
if (null == $l2) return $l1;
$list = new ListNode(0);
$cur = $list;
while (null != $l1 && null != $l2) {
if ($l1->val < $l2->val) {
$cur->next = $l1;
$cur = $cur->next;
$l1 = $l1->next;
} else {
$cur->next = $l2;
$cur = $cur->next;
$l2 = $l2->next;
}
}
if ($l1 == null) {
$cur->next = $l2;
}
if ($l2 == null) {
$cur->next = $l1;
}
return $list->next;
}
}
class ListNode {
public $val = 0;
public $next = null;
function __construct($val) { $this->val = $val; }
}