Leetcode/cpp/83.cpp
2024-06-21 01:36:27 +01:00

35 lines
985 B
C++

// https://leetcode.com/problems/remove-duplicates-from-sorted-list/
#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* deleteDuplicates(ListNode* head) {
if (head == nullptr)
return head;
bool newSequence = false;
int currentVal = head->val;
ListNode* front = head->next;
ListNode* rear = head;
while (front != nullptr) {
if (front->val != currentVal) {
std::cout << "Previous value: " << currentVal << "; current value: " << front->val << '\n';
rear->next = front;
currentVal = front->val;
rear = front;
}
front = front->next;
}
rear->next = nullptr;
return head;
}
};