mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge pull request #1504 from LinStan/update/listnode_ts
修正:(链表章节-> 移除链表元素)TS虚拟头节点版本代码错误
This commit is contained in:
@ -397,18 +397,18 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
|
||||
|
||||
```typescript
|
||||
function removeElements(head: ListNode | null, val: number): ListNode | null {
|
||||
let dummyHead = new ListNode(0, head);
|
||||
let pre: ListNode = dummyHead, cur: ListNode | null = dummyHead.next;
|
||||
// 删除非头部节点
|
||||
// 添加虚拟节点
|
||||
const data = new ListNode(0, head);
|
||||
let pre = data, cur = data.next;
|
||||
while (cur) {
|
||||
if (cur.val === val) {
|
||||
pre.next = cur.next;
|
||||
pre.next = cur.next
|
||||
} else {
|
||||
pre = cur;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
return head.next;
|
||||
return data.next;
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user