-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeTwoSortedLists.php
More file actions
65 lines (45 loc) · 1.13 KB
/
mergeTwoSortedLists.php
File metadata and controls
65 lines (45 loc) · 1.13 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
55
56
57
58
59
60
61
62
63
64
65
<?php
class ListNode {
public $val = 0;
public $next = null;
function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
/**
* 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
* 注意:本题与主站 21 题相同:https://leetcode-cn.com/problems/merge-two-sorted-lists/
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function mergeTwoLists($l1, $l2) {
if($l1 === null)
return $l2;
if($l2 === null)
return $l1;
$p1 = $l1;
$p2 = $l2;
$head = new ListNode(null);
$tail = $head;
while ($p1 !== null && $p2 !== null){
//比较值大小,链接到结果链表尾部
if($p1->val <= $p2->val){
$tail->next = $p1;
$tail = $p1;
$p1 = $p1->next;
}else{
$tail->next = $p2;
$tail = $p2;
$p2 = $p2->next;
}
}
if($p1 !== null){
$tail->next = $p1;
}
if($p2 !== null){
$tail->next = $p2;
}
return $head->next;
}