mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
添加 0203.移除链表元素 PHP版本
This commit is contained in:
@ -331,8 +331,32 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
// 虚拟头+双指针
|
||||
func removeElements(head *ListNode, val int) *ListNode {
|
||||
dummyHead := &ListNode{}
|
||||
dummyHead.Next = head
|
||||
pred := dummyHead
|
||||
cur := head
|
||||
for cur != nil {
|
||||
if cur.Val == val {
|
||||
pred.Next = cur.Next
|
||||
} else {
|
||||
pred = cur
|
||||
}
|
||||
cur = cur.Next
|
||||
}
|
||||
return dummyHead.Next
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
Reference in New Issue
Block a user