您的位置:首页 > 编程语言 > C语言/C++

leetcode 日经贴,Cpp code -Remove Duplicates from Sorted List II

2015-04-18 16:57 441 查看
Remove Duplicates from Sorted List II

/**
* 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 || !head->next) return head;
ListNode *ch = NULL, *cp = NULL;
while (head) {
if (!head->next || head->val != head->next->val) {
if(!ch) {
ch = cp = head;
} else {
cp->next = head;
cp = head;
}
head = head->next;
} else {
int val = head->val;
while (head && head->val == val) {
head = head->next;
}
}
}
if(cp) {
cp->next = NULL;
}
return ch;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: