/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (!head) return NULL;
ListNode* prev = head;
ListNode* cur = head->next;
if (!cur) return head;
while (cur)
{
if (cur->val == prev->val)
{
prev->next = cur->next;
delete cur;
cur = prev->next;
}
else
{
prev = cur;
cur = cur->next;
}
}
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (!head) return NULL;
ListNode** front = &head;
ListNode* cur = head->next;
while (cur)
{
if (cur->val != (*front)->val)
{
front = &(*front)->next;
*front = cur;
}
cur = cur->next;
}
front = &(*front)->next;
*front = NULL;
return head;
}
};
711

被折叠的 条评论
为什么被折叠?



