-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path82.cpp
More file actions
52 lines (42 loc) · 1.36 KB
/
82.cpp
File metadata and controls
52 lines (42 loc) · 1.36 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
// Problem : 82. Remove Duplicates from Sorted List II
// Link : https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
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 *deleteDuplicates(ListNode *head) {
ListNode *sentinal = new ListNode(-200, head);
ListNode *prev = sentinal;
while (head) {
if (head->next && head->next->val == head->val) {
while (head->next && head->val == head->next->val) {
head = head->next;
}
prev->next = head->next;
} else
prev = prev->next;
head = head->next;
}
return sentinal->next;
}
};
int main() {
// ListNode head = ListNode(1, new ListNode(2, new ListNode(3, new ListNode(3, new ListNode(4, new ListNode(4, new ListNode(5, nullptr)))))));
ListNode head = ListNode(1, new ListNode(1, nullptr));
Solution ob;
ListNode *output = ob.deleteDuplicates(&head);
while (output != nullptr) {
cout << output->val << endl;
output = output->next;
}
return 0;
}