-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathQ2_01_Remove_Dups.cs
151 lines (129 loc) · 4.32 KB
/
Q2_01_Remove_Dups.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using ctci.Contracts;
using ctci.Library;
using System;
using System.Collections.Generic;
namespace Chapter02
{
public class Q2_01_Remove_Dups : Question
{
private int _tapB = 0;
private int _tapC = 0;
private void Tap(int i)
{
if (i == 0)
{
_tapB++;
}
else
{
_tapC++;
}
}
private void DeleteDupsA(LinkedListNode node)
{
var table = new HashSet<int>();
LinkedListNode previous = null;
while (node != null)
{
if (table.Contains(node.Data))
{
if (previous != null)
{
previous.Next = node.Next;
}
}
else
{
table.Add(node.Data);
previous = node;
}
node = node.Next;
}
}
private void DeleteDupsB(LinkedListNode head)
{
if (head == null) return;
var current = head;
while (current != null)
{
/* Remove all future nodes that have the same value */
var runner = current;
while (runner.Next != null)
{
Tap(0);
if (runner.Next.Data == current.Data)
{
runner.Next = runner.Next.Next;
}
else
{
runner = runner.Next;
}
}
current = current.Next;
}
}
private void DeleteDupsC(LinkedListNode head)
{
if (head == null) return;
var previous = head;
var current = previous.Next;
while (current != null)
{
// Look backwards for dups, and remove any that you see.
var runner = head;
while (runner != current)
{
Tap(1);
if (runner.Data == current.Data)
{
var temp = current.Next;
previous.Next = temp;
current = temp;
/* We know we can't have more than one dup preceding
* our element since it would have been removed
* earlier. */
break;
}
runner = runner.Next;
}
/* If runner == current, then we didn�t find any duplicate
* elements in the previous for loop. We then need to
* increment current.
* If runner != current, then we must have hit the �break�
* condition, in which case we found a dup and current has
* already been incremented.*/
if (runner == current)
{
previous = current;
current = current.Next;
}
}
}
public override void Run()
{
var first = new LinkedListNode(0, null, null);
var originalList = first;
var second = first;
for (var i = 1; i < 8; i++)
{
second = new LinkedListNode(i % 2, null, null);
first.SetNext(second);
second.SetPrevious(first);
first = second;
}
var list1 = originalList.Clone();
var list2 = originalList.Clone();
var list3 = originalList.Clone();
DeleteDupsA(list1);
DeleteDupsB(list2);
DeleteDupsC(list3);
Console.WriteLine(originalList.PrintForward());
Console.WriteLine(list1.PrintForward());
Console.WriteLine(list1.PrintForward());
Console.WriteLine(list1.PrintForward());
Console.WriteLine(_tapB);
Console.WriteLine(_tapC);
}
}
}