-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83-remove-duplicates-from-sorted-list.cpp
96 lines (86 loc) · 2.43 KB
/
83-remove-duplicates-from-sorted-list.cpp
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
#include <iomanip>
#include <chrono>
#include <iostream>
struct ListNode {
int val;
ListNode* next;
ListNode()
: val(0), next(nullptr) {}
ListNode(int x)
: val(x), next(nullptr) {}
ListNode(int x, ListNode* next)
: val(x), next(next) {}
};
class Solution {
public:
ListNode* insert(ListNode* head, int val) {
if (head == nullptr)
head = new ListNode(val);
else {
ListNode* tail = head;
while (tail && tail->next != nullptr)
tail = tail->next;
tail->next = new ListNode(val);
}
return head;
}
void display(ListNode* head) {
ListNode* start = head;
while (start) {
std::cout << start->val << " ";
start = start->next;
}
}
// first attempt
ListNode* deleteDuplicates(ListNode* head) {
if (head == nullptr) return head;
ListNode* first = head;
ListNode* temp = head;
int pval = head->val;
head = head->next;
while (head) {
if (head->val != pval) {
pval = head->val;
temp = head;
}
else {
temp->next = head->next;
}
head = head->next;
}
return first;
}
// LeetCode best performing solution WRONG
// with a linked list of 5 elements {1 1 2 3 3 }
// the measured runtime is: 0.001289 ms
// my solution is faster: 0.00092000000000000003 ms
ListNode* deleteDuplicates2(ListNode* head) {
ListNode *ans = new ListNode(0);
ListNode *curr = ans;
while(head) {
while(head->next && head -> val == head -> next -> val)
head =head -> next;
curr -> next = head;
curr = head;
head = head -> next;
}
return ans->next;
}
};
int main() {
ListNode* head = nullptr;
Solution sol;
int n, val;
std::cin >> n;
while (n-- > 0) {
std::cin >> val;
head = sol.insert(head, val);
}
auto t1 = std::chrono::high_resolution_clock::now();
head = sol.deleteDuplicates(head);
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> ms_double = t2 - t1;
std::cout << std::setprecision(17) << ms_double.count() << " ms\n";
sol.display(head);
return 0;
}