LeetCode 83. Remove Duplicates from Sorted List
原题链接
简单
作者:
YC.
,
2020-07-22 23:35:07
,
所有人可见
,
阅读 2
/**
* Definition for singly-linked list.
* 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* dumyHead = new ListNode(512);
dumyHead->next = head;
ListNode* preHead = dumyHead;
ListNode* currentHead = head;
while(currentHead != NULL)
{
if(preHead->val == currentHead->val)
{
preHead->next = currentHead->next;
currentHead = preHead->next;
}
else
{
preHead = preHead->next;
currentHead = currentHead->next;
}
}
return dumyHead->next;
}
}