leetcode_remove_duplicates_from_sorted_list

难度:Easy

注意事项:先写一个dummy,指向head元素。问题会简单很多。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 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) {
ListNode *dummy = new ListNode(-1), *current = dummy;
dummy->next = head;
while(current->next != NULL && current->next->next != NULL)
{
if(current->next->val == current->next->next->val)
{
current->next->next = current->next->next->next;
}
else
{
current = current->next;
}
}
return dummy->next;
}
};

运行结果:13ms,超过24.84%