-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path83.cpp
More file actions
46 lines (37 loc) · 1.13 KB
/
83.cpp
File metadata and controls
46 lines (37 loc) · 1.13 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
// Problem : 83. Remove Duplicates from Sorted List
// Link : https://leetcode.com/problems/remove-duplicates-from-sorted-list/
#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* curr = head;
while(curr && curr->next ){
if(curr->val == curr->next->val)
curr->next = curr->next->next;
else
curr = curr->next;
}
return head;
}
};
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(2, nullptr));
Solution ob;
ListNode *output = ob.deleteDuplicates(&head);
while (output != nullptr) {
cout << output->val << endl;
output = output->next;
}
return 0;
}