Remove Duplicates from Sorted List II
2015-05-24 19:42
204 查看
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *head1=head ,*p,*pre=head;
int i,count=0;
if(head==NULL||head->next==NULL) return head;
while(head->next->val==head->val)
{ i=head->val;
while(i==head->val&&head->next!=NULL)
{
head=head->next;
count=1;
} //找出头结点,把头结点重复的都删掉。
if(count==1&&head->val==i) return NULL;
// if(head->next==NULL) return NULL;
if(head->next==NULL) return head; //判断是否头结点后面就只剩一个元素
}
head1=head; //重新开始找
if(count==1) pre=head;
//if(head->next!=NULL&&count==1) head=head->next;
while(head1->next!=NULL&&head1->next->next!=NULL) //最后就会剩一个元素
{
if(head1->next->val==head1->val) //相等的话保存值继续找
{
i=head1->val;
// flag=1;
head1=head1->next->next;
while(i==head1->val&&head1->next!=NULL)
{ head1=head1->next;} //找出不等的
if(head1->next!=NULL&&head1->next->val!=head1->val) //接上新的头结点
{
head->next=head1;
head=head->next;
}
if(head1->next==NULL&&head1->val==i) 判断是否结束
head->next=NULL;
// else head->next=head1;
if(head1->next==NULL&&head1->val!=i)
head->next=head1;
}
else {
head1=head1->next;
if(head1->next!=NULL&&head1->next->val!=head1->val) 要判断是否该接入。
{
head->next=head1;
head=head->next;
}
// head1=head1->next;
}
}
if(head1->next!=NULL&&(head1->val==head1->next->val))
{ head->next=NULL;}
return pre;
}
};
int main() {
Solution solution;
int A[] = {1,2,2,3};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 0;i <=3;i++){
node = new ListNode(0);
node->val= A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
//for(int i = 0;i <=0;i++){
// node = new ListNode(1);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
// for(int i = 0;i <=1;i++){
// node = new ListNode(2);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
//for(int i = 0;i <=2;i++){
// node = new ListNode(3);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
// // pre = head2;
// //for(int i = 2;i < 5;i++){
// // node = new ListNode(4);
// // // node->val = B[i];
// // node->next = NULL;
// // pre->next = node;
// // pre = node;
// //}
head = solution.deleteDuplicates(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
}
满纸荒唐言,一把辛酸泪啊。花了我快三天。
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *head1=head ,*p,*pre=head;
int i,count=0;
if(head==NULL||head->next==NULL) return head;
while(head->next->val==head->val)
{ i=head->val;
while(i==head->val&&head->next!=NULL)
{
head=head->next;
count=1;
} //找出头结点,把头结点重复的都删掉。
if(count==1&&head->val==i) return NULL;
// if(head->next==NULL) return NULL;
if(head->next==NULL) return head; //判断是否头结点后面就只剩一个元素
}
head1=head; //重新开始找
if(count==1) pre=head;
//if(head->next!=NULL&&count==1) head=head->next;
while(head1->next!=NULL&&head1->next->next!=NULL) //最后就会剩一个元素
{
if(head1->next->val==head1->val) //相等的话保存值继续找
{
i=head1->val;
// flag=1;
head1=head1->next->next;
while(i==head1->val&&head1->next!=NULL)
{ head1=head1->next;} //找出不等的
if(head1->next!=NULL&&head1->next->val!=head1->val) //接上新的头结点
{
head->next=head1;
head=head->next;
}
if(head1->next==NULL&&head1->val==i) 判断是否结束
head->next=NULL;
// else head->next=head1;
if(head1->next==NULL&&head1->val!=i)
head->next=head1;
}
else {
head1=head1->next;
if(head1->next!=NULL&&head1->next->val!=head1->val) 要判断是否该接入。
{
head->next=head1;
head=head->next;
}
// head1=head1->next;
}
}
if(head1->next!=NULL&&(head1->val==head1->next->val))
{ head->next=NULL;}
return pre;
}
};
int main() {
Solution solution;
int A[] = {1,2,2,3};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 0;i <=3;i++){
node = new ListNode(0);
node->val= A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
//for(int i = 0;i <=0;i++){
// node = new ListNode(1);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
// for(int i = 0;i <=1;i++){
// node = new ListNode(2);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
//for(int i = 0;i <=2;i++){
// node = new ListNode(3);
// // node->exp = A[i];
// node->next = NULL;
// pre->next = node;
// pre = node;
// }
// // pre = head2;
// //for(int i = 2;i < 5;i++){
// // node = new ListNode(4);
// // // node->val = B[i];
// // node->next = NULL;
// // pre->next = node;
// // pre = node;
// //}
head = solution.deleteDuplicates(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
}
满纸荒唐言,一把辛酸泪啊。花了我快三天。
相关文章推荐
- [Leetcode]Remove Duplicates from Sorted List II
- leetcode 82: Remove Duplicates from Sorted List II
- Remove duplicates from sorted list II
- 链表 Remove Duplicates from Sorted List II
- remove duplicates from sorted list II
- 【LeetCode】Remove Duplicates from Sorted List & Remove Duplicates from Sorted List II
- LeetCode-82-Remove Duplicates from Sorted List II(链表)-Medium
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
- 82 Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List II
- LeetCode 82 Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- 【链表】Remove Duplicates from Sorted List II(三指针)
- Remove Duplicates from Sorted List II
- 31:Remove Duplicates from Sorted List II
- [Leetcode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II