-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
50 lines (39 loc) · 842 Bytes
/
solution.ts
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
/*
* @lc app=leetcode id=203 lang=javascript
*
* [203] Remove Linked List Elements
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
type MaybeList = ListNode | null;
interface ListNode {
val: number;
next: MaybeList;
}
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
const removeElements = (head: MaybeList, val: number): MaybeList => {
// * ['60 ms', '99.36 %', '36.9 MB', '62.5 %']
if (head === null) return head;
const dummy = { next: head } as ListNode;
let cur: MaybeList = dummy;
while (cur.next) {
if (cur!.next.val === val) {
cur.next = cur.next.next;
continue;
}
cur = cur.next;
}
return dummy.next;
};
// @lc code=end
export { removeElements };