Skip to content

Commit b4c6130

Browse files
committed
添加 0203.移除链表元素 PHP版本
1 parent b8ef037 commit b4c6130

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

problems/0203.移除链表元素.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,32 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
331331
}
332332
```
333333

334-
335-
334+
PHP:
335+
```php
336+
/**
337+
* Definition for singly-linked list.
338+
* type ListNode struct {
339+
* Val int
340+
* Next *ListNode
341+
* }
342+
*/
343+
// 虚拟头+双指针
344+
func removeElements(head *ListNode, val int) *ListNode {
345+
dummyHead := &ListNode{}
346+
dummyHead.Next = head
347+
pred := dummyHead
348+
cur := head
349+
for cur != nil {
350+
if cur.Val == val {
351+
pred.Next = cur.Next
352+
} else {
353+
pred = cur
354+
}
355+
cur = cur.Next
356+
}
357+
return dummyHead.Next
358+
}
359+
```
336360

337361
-----------------------
338362
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)