Files
Electrominch c019e8856c Day7
2022-10-08 01:07:50 +03:00

30 lines
687 B
C#

namespace _203._Remove_Linked_List_Elements;
public class Solution
{
public ListNode? RemoveElements(ListNode? head, int val)
{
ListNode? first = null;
ListNode? current = null;
while (head != null)
{
if (head.val == val)
{
head = head.next;
continue;
}
if (first == null)
first = current = head;
else
{
current!.next = head;
current = current.next;
}
var buf = head;
head = head.next;
buf.next = null;
}
return first;
}
}