-
Notifications
You must be signed in to change notification settings - Fork 2
/
tkd9714.php
48 lines (42 loc) · 964 Bytes
/
tkd9714.php
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
/**
* 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) {
$node = new ListNode(0);
$link = $node;
while ($l1!==null && $l2!==null)
{
if ($l1->val < $l2->val)
{
$link->next = $l1;
$l1 = $l1->next;
}
else
{
$link->next = $l2;
$l2 = $l2->next;
}
$link = $link->next;
}
if ($l1 === null)
{
$link->next = $l2;
}
else
{
$link->next = $l1;
}
return $node->next;
}
}