This commit is contained in:
Electrominch
2022-10-11 01:45:16 +03:00
parent c019e8856c
commit 02afed9c4c
34 changed files with 691 additions and 3 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_83._Remove_Duplicates_from_Sorted_List</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,37 @@
namespace _83._Remove_Duplicates_from_Sorted_List;
//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 static ListNode Create(int[] nums)
{
ListNode l = new ListNode(nums[0]);
ListNode cur = l;
for (int i = 1; i < nums.Length; i++)
{
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
return l;
}
public override string ToString()
{
List<int> list = new List<int>();
ListNode? cur = this;
while (cur != null)
{
list.Add(cur.val);
cur = cur.next;
}
return String.Join(" ", list);
}
}

View File

@@ -0,0 +1,17 @@
namespace _83._Remove_Duplicates_from_Sorted_List;
internal class Program
{
static void Main(string[] args)
{
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(1);
ListNode l3 = new ListNode(4);
ListNode l4 = new ListNode(4);
l1.next = l2;
l2.next = l3;
l3.next = l4;
Console.WriteLine(new Solution().DeleteDuplicates(null));
}
}

View File

@@ -0,0 +1,26 @@
namespace _83._Remove_Duplicates_from_Sorted_List;
public class Solution
{
public ListNode? DeleteDuplicates(ListNode? head)
{
ListNode? first = null;
ListNode? current = null;
while (head != null)
{
if (first == null)
first = current = head;
if (head.val == current!.val)
{
head = head.next;
continue;
}
current!.next = head;
current = current.next;
head = head.next;
}
if(current != null)
current.next = null;
return first;
}
}