-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy path4.php
More file actions
32 lines (28 loc) · 727 Bytes
/
4.php
File metadata and controls
32 lines (28 loc) · 727 Bytes
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
<?php
/*
* 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
*/
/*class ListNode{
var $val;
var $next = NULL;
function __construct($x){
$this->val = $x;
}
}*/
function Merge($pHead1, $pHead2)
{
if (is_null($pHead1)) {
return $pHead2;
} elseif (is_null($pHead2)) {
return $pHead1;
}
$merged = new ListNode(null);
if ($pHead1->val < $pHead2->val) {
$merged->val = $pHead1->val;
$merged->next = Merge($pHead1->next, $pHead2);
} else {
$merged->val = $pHead2->val;
$merged->next = Merge($pHead1, $pHead2->next);
}
return $merged;
}