Remove Duplicates from Sorted List II
2015-07-31 11:21
295 查看
/** * 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) return head; if(head->next ==NULL) return head; ListNode *res = head; int head_val; while(head){ head_val = head->val; if(head->next && head ->next->val == head_val){ while(head && head->val == head_val) //去掉开头重复的 { head =head->next; } } else break; } res=head; //如果重复的部分不是在开头,需要记录pre结点。这里特别要注意逻辑性,head只有在后续结点不是重复结点的情况下,才可以往后移动 while(head){ if(head->next == NULL) break; ListNode* p_i = head->next; int next_val = p_i->val; if(p_i!=NULL &&p_i->next!=NULL && p_i->next->val == next_val){ while(p_i && p_i->val ==next_val) { p_i =p_i->next; } if(p_i == NULL) {head->next=NULL;break;} head->next = p_i; } else head = p_i; } return res; } };
相关文章推荐
- dom
- 使用 NIO.2 遍历目录下所有的Java文件
- 阿拉伯数字转为中文读法
- 微信二维码1-服务号推广(将网页存储为图片存储到服务器)
- linux 下的top 、free的性能分析详解
- 欢迎使用CSDN-markdown编辑器
- vsix无法打开的问题
- apache 开启zgip 压缩模式
- thinkphp
- JS 页面加载触发事件 document.ready和onload的区别
- WCF 配置文件
- 7、8上的cell上的一个按钮,当点击按钮时,要拿到这个cell,可以用代理,也可以用superview
- AutoMapper使用
- EBP详解
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- android:button点击
- “浏览过该商品的用户还浏览了”的商品推荐
- Oracle数据库操作中锁表查询以及解锁和kill进程操作脚本
- sudo su -命令介绍
- 偶尔见到的perl的小常识