-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0082-Remove-duplicates-from-sorted-list-II.cs
More file actions
59 lines (49 loc) · 1.55 KB
/
0082-Remove-duplicates-from-sorted-list-II.cs
File metadata and controls
59 lines (49 loc) · 1.55 KB
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
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0082.Remove_duplicates_from_sorted_list_II
{
public class _0082_Remove_duplicates_from_sorted_list_II
{
public ListNode DeleteDuplicates(ListNode head)
{
if (head == null) return null;
ListNode res = new ListNode();
ListNode prev = res;
res.next = head;
while (head != null)
{
while (head.next != null && head.val == head.next.val)
head = head.next;
if (prev.next == head)
prev = prev.next;
else
prev.next = head.next;
head = head.next;
}
return res.next;
// easy
//if (head == null) return head;
//ListNode temp = new ListNode();
//ListNode res = temp;
//Dictionary<int, int> dic = new Dictionary<int, int>();
//// because the head is sorted list.
//while (head != null)
//{
// if (!dic.TryAdd(head.val, 1))
// dic[head.val]++;
// head = head.next;
//}
//foreach (KeyValuePair<int, int> pair in dic)
//{
// if (pair.Value == 1)
// {
// temp.next = new ListNode(pair.Key);
// temp = temp.next;
// }
//}
//return res.next;
}
}
}