-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0082-RemoveDuplicatesFromSortedListII.cs
98 lines (95 loc) · 2.33 KB
/
0082-RemoveDuplicatesFromSortedListII.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using Xunit;
using Util;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace RemoveDuplicatesFromSortedListII
{
public class Solution
{
public ListNode DeleteDuplicates(ListNode head)
{
if (head == null)
{
return head;
}
while ((head.next != null) && (head.next.val == head.val))
{
var x = head.val;
var find = false;
while (head.next != null)
{
head = head.next;
if (head.val != x)
{
find = true;
break;
}
}
if (!find)
{
return null;
}
}
ListNode lastprev = head;
ListNode last = head;
ListNode prev = head;
ListNode curr = head;
while (curr.next != null)
{
prev = curr;
curr = curr.next;
if (curr.val == prev.val)
{
last = lastprev;
last.next = null;
}
else
{
last.next = curr;
lastprev = last;
last = curr;
}
}
return head;
}
}
public class Test
{
static void Verify(ListNode head, ListNode exp)
{
Console.WriteLine($"{head}");
ListNode res;
using (new Timeit())
{
res = new Solution().DeleteDuplicates(head);
}
Assert.Equal(exp, res);
}
static public void Run()
{
Console.WriteLine(typeof(Solution).Namespace);
var input = @"
#[1,2,3,3,4,4,5]
#[1,2,5]
#[1,1,1,2,3]
#[2,3]
[1,1,2,2]
[]
#[1,1]
#[]
";
var lines = input.CleanInput();
ListNode head;
ListNode exp;
int idx = 0;
while (idx < lines.Length)
{
head = lines[idx++].JsonToListNode();
exp = lines[idx++].JsonToListNode();
Verify(head, exp);
}
}
}
}