给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
/**
* 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) {
if (head == NULL ||head->next ==
NULL)
{
return head;
}
ListNode *test =
head;
while (test->next!=
NULL)
{
if (test->val == (test->next)->
val)
test->next = test->next->
next;
else
test = test->
next;
}
return head;
}
};
转载于:https://www.cnblogs.com/qian-lu/p/9682441.html