-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0203-RemoveLinkedListElements.cs
39 lines (37 loc) · 1.08 KB
/
0203-RemoveLinkedListElements.cs
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
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 27.8 MB
// Link: https://leetcode.com/submissions/detail/358364330/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class _0203_RemoveLinkedListElements
{
public ListNode RemoveElements(ListNode head, int val)
{
var dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy, curr = head;
while (curr != null)
{
if (curr.val == val)
prev.next = curr.next;
else
prev = prev.next;
curr = curr.next;
}
return dummy.next;
}
}
}