Skip to content

Commit 29f8671

Browse files
authored
feat: add php solutions to lc problems: No.0876,1290 (#893)
* No.0876.Middle of the Linked List * No.1290.Convert Binary Number in a Linked List to Integer
1 parent 173ebad commit 29f8671

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

solution/0800-0899/0876.Middle of the Linked List/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,43 @@ struct ListNode *middleNode(struct ListNode *head) {
223223
}
224224
```
225225
226+
### **PHP**
227+
228+
```php
229+
/**
230+
* Definition for a singly-linked list.
231+
* class ListNode {
232+
* public $val = 0;
233+
* public $next = null;
234+
* function __construct($val = 0, $next = null) {
235+
* $this->val = $val;
236+
* $this->next = $next;
237+
* }
238+
* }
239+
*/
240+
class Solution {
241+
242+
/**
243+
* @param ListNode $head
244+
* @return ListNode
245+
*/
246+
function middleNode($head) {
247+
$count = 0;
248+
$tmpHead = $head;
249+
while ($tmpHead != null) {
250+
$tmpHead = $tmpHead->next;
251+
$count++;
252+
}
253+
$len = $count - floor($count / 2);
254+
while ($count != $len) {
255+
$head = $head->next;
256+
$count--;
257+
}
258+
return $head;
259+
}
260+
}
261+
```
262+
226263
### **...**
227264

228265
```

solution/0800-0899/0876.Middle of the Linked List/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,42 @@ struct ListNode *middleNode(struct ListNode *head) {
204204
}
205205
```
206206
207+
### **PHP**
208+
209+
```php
210+
/**
211+
* Definition for a singly-linked list.
212+
* class ListNode {
213+
* public $val = 0;
214+
* public $next = null;
215+
* function __construct($val = 0, $next = null) {
216+
* $this->val = $val;
217+
* $this->next = $next;
218+
* }
219+
* }
220+
*/
221+
class Solution {
222+
/**
223+
* @param ListNode $head
224+
* @return ListNode
225+
*/
226+
function middleNode($head) {
227+
$count = 0;
228+
$tmpHead = $head;
229+
while ($tmpHead != null) {
230+
$tmpHead = $tmpHead->next;
231+
$count++;
232+
}
233+
$len = $count - floor($count / 2);
234+
while ($count != $len) {
235+
$head = $head->next;
236+
$count--;
237+
}
238+
return $head;
239+
}
240+
}
241+
```
242+
207243
### **...**
208244

209245
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a singly-linked list.
3+
* class ListNode {
4+
* public $val = 0;
5+
* public $next = null;
6+
* function __construct($val = 0, $next = null) {
7+
* $this->val = $val;
8+
* $this->next = $next;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
/**
14+
* @param ListNode $head
15+
* @return ListNode
16+
*/
17+
function middleNode($head) {
18+
$count = 0;
19+
$tmpHead = $head;
20+
while ($tmpHead != null) {
21+
$tmpHead = $tmpHead->next;
22+
$count++;
23+
}
24+
$len = $count - floor($count / 2);
25+
while ($count != $len) {
26+
$head = $head->next;
27+
$count--;
28+
}
29+
return $head;
30+
}
31+
}

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ int getDecimalValue(struct ListNode *head) {
259259
}
260260
```
261261
262+
### **PHP**
263+
264+
```PHP
265+
/**
266+
* Definition for a singly-linked list.
267+
* class ListNode {
268+
* public $val = 0;
269+
* public $next = null;
270+
* function __construct($val = 0, $next = null) {
271+
* $this->val = $val;
272+
* $this->next = $next;
273+
* }
274+
* }
275+
*/
276+
class Solution {
277+
278+
/**
279+
* @param ListNode $head
280+
* @return Integer
281+
*/
282+
function getDecimalValue($head) {
283+
$rs = array();
284+
while ($head != null) {
285+
array_push($rs, $head->val);
286+
$head = $head->next;
287+
}
288+
$rsStr = implode($rs);
289+
return bindec($rsStr);
290+
}
291+
}
292+
```
293+
262294
### **...**
263295

264296
```

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,38 @@ int getDecimalValue(struct ListNode *head) {
225225
}
226226
```
227227
228+
### **PHP**
229+
230+
```PHP
231+
/**
232+
* Definition for a singly-linked list.
233+
* class ListNode {
234+
* public $val = 0;
235+
* public $next = null;
236+
* function __construct($val = 0, $next = null) {
237+
* $this->val = $val;
238+
* $this->next = $next;
239+
* }
240+
* }
241+
*/
242+
class Solution {
243+
244+
/**
245+
* @param ListNode $head
246+
* @return Integer
247+
*/
248+
function getDecimalValue($head) {
249+
$rs = array();
250+
while ($head != null) {
251+
array_push($rs, $head->val);
252+
$head = $head->next;
253+
}
254+
$rsStr = implode($rs);
255+
return bindec($rsStr);
256+
}
257+
}
258+
```
259+
228260
### **...**
229261

230262
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a singly-linked list.
3+
* class ListNode {
4+
* public $val = 0;
5+
* public $next = null;
6+
* function __construct($val = 0, $next = null) {
7+
* $this->val = $val;
8+
* $this->next = $next;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
14+
/**
15+
* @param ListNode $head
16+
* @return Integer
17+
*/
18+
function getDecimalValue($head) {
19+
$rs = array();
20+
while ($head != null) {
21+
array_push($rs, $head->val);
22+
$head = $head->next;
23+
}
24+
$rsStr = implode($rs);
25+
return bindec($rsStr);
26+
}
27+
}

0 commit comments

Comments
 (0)