-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cs
43 lines (40 loc) · 928 Bytes
/
Solution.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
public class Solution {
private ListNode newHead;
private ListNode last;
private ListNode candidate;
private int count;
public ListNode DeleteDuplicates(ListNode head) {
while (head != null)
{
if (candidate == null || candidate.val != head.val)
{
TryAppend();
candidate = head;
count = 1;
}
else
{
++count;
}
head = head.next;
}
TryAppend();
if (last != null) last.next = null;
return newHead;
}
private void TryAppend()
{
if (count == 1)
{
if (newHead == null)
{
newHead = last = candidate;
}
else
{
last.next = candidate;
last = last.next;
}
}
}
}