LeetCode 83. Remove Duplicates from Sorted List
Solution1:我的答案
思路和82题差不多,此算法还算是比较好的!
/**
* 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) {
struct ListNode* cur = head;
if (cur == NULL || cur->next == NULL)
return head;
while (cur->next) {
if (cur->val == cur->next->val)
cur->next = cur->next->next;
else
cur = cur->next;
}
return head;
}
};