-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.合并两个有序链表.php
149 lines (133 loc) · 3.22 KB
/
21.合并两个有序链表.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* @lc app=leetcode.cn id=21 lang=php
*
* [21] 合并两个有序链表
*
* https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
*
* algorithms
* Easy (65.89%)
* Likes: 1689
* Dislikes: 0
* Total Accepted: 553.8K
* Total Submissions: 840.3K
* Testcase Example: '[1,2,4]\n[1,3,4]'
*
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
*
*
*
* 示例 1:
*
*
* 输入:l1 = [1,2,4], l2 = [1,3,4]
* 输出:[1,1,2,3,4,4]
*
*
* 示例 2:
*
*
* 输入:l1 = [], l2 = []
* 输出:[]
*
*
* 示例 3:
*
*
* 输入:l1 = [], l2 = [0]
* 输出:[0]
*
*
*
*
* 提示:
*
*
* 两个链表的节点数目范围是 [0, 50]
* -100
* l1 和 l2 均按 非递减顺序 排列
*
*
*/
// @lc code=start
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution
{
/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
public function mergeTwoLists($l1, $l2)
{
if ($l1 === null) {
return $l2 === null ? [] : $l2;
}
if ($l2 === null) {
return $l1 === null ? [] : $l1;
}
$a = null;
$p = null;
while ($l1 !== null && $l2 !== null) {
while ($l2 !== null) {
// 相等的情况, 放入 l1, l2
if ($l1->val === $l2->val) {
if ($a === null) {
$a = $p = new ListNode($l1->val);
$a->next = new ListNode($l2->val);
$a = $a->next;
} else {
$a->next = new ListNode($l1->val);
$a = $a->next;
$a->next = new ListNode($l2->val);
$a = $a->next;
}
$l1 = $l1->next;
$l2 = $l2->next;
break;
}
// l1 大于 l2, 放入 l2, 继续循环 l2
if ($l1->val > $l2->val) {
if ($a === null) {
$a = $p = new ListNode($l2->val);
} else {
$a->next = new ListNode($l2->val);
$a = $a->next;
}
$l2 = $l2->next;
continue;
}
// l1 小于 l2, 放入 l1, 循环 l1
if ($l1->val < $l2->val) {
if ($a === null) {
$a = $p = new ListNode($l1->val);
} else {
$a->next = new ListNode($l1->val);
$a = $a->next;
}
$l1 = $l1->next;
break;
}
}
}
if ($l1 !== null) {
$a->next = $l1;
}
if ($l2 !== null) {
$a->next = $l2;
}
return $p;
}
}
// @lc code=end