Remove Duplicates from Sorted List I & II
Title:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
/**
* 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* p = head;
ListNode* pre = NULL;
while (p != NULL){
if (pre != NULL && p->val == pre->val){
pre->next = p->next;
delete p;
p = pre->next;
}
else{
pre = p;
p = p->next;
}
}
return head;
}
};
Title:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
class Solution{
    public:
ListNode *deleteDuplicates(ListNode *head) {
        if(head == NULL || head->next == NULL){
            return head;
        }
        ListNode *p = new ListNode(-);
        p->next = head;
        ListNode *cur = p, *pre = head;
        while(pre != NULL){
            bool isDupli = false;
            while(pre->next != NULL && pre->val == pre->next->val){
                isDupli = true;
                pre = pre->next;
            }
            if(isDupli){
                pre = pre->next;
                continue;
            }
            cur->next = pre;
            cur = cur->next;
            pre = pre->next;
        }
        cur->next = pre;
        return p->next;
    }
};
Remove Duplicates from Sorted List I & II的更多相关文章
- LeetCode:Remove Duplicates from Sorted List I II
		LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ... 
- LeetCode:Remove Duplicates from Sorted Array I II
		LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ... 
- Remove Duplicates from Sorted Array I&&II——怎样防超时
		Given a sorted array, remove the duplicates in place such that each element appear only once and ret ... 
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
		Given a sorted array, remove the duplicates in place such that each element appear only once and ret ... 
- Remove Duplicates from Sorted List II
		Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ... 
- 【leetcode】Remove Duplicates from Sorted Array II
		Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ... 
- 50. Remove Duplicates from Sorted Array  &&  Remove Duplicates from Sorted Array II  &&  Remove Element
		Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ... 
- 48. Remove Duplicates from Sorted List  &&  Remove Duplicates from Sorted List II
		Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ... 
- 【LeetCode练习题】Remove Duplicates from Sorted List II
		Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ... 
随机推荐
- Node.js 随记
			http://nodejs.org/ 下载并安装 node.js 最新版本 运行cmd,输入npm install xxxxxx 回车,安装扩展模块,如:express,socket.io等 运行c ... 
- Eclipse插件开发
			最近在做Eclipse的插件开发,目前是在Eclipse3.x环境上进行开发,之后迁移到Eclipse4.x环境.会贡献在插件开发过程中遇到的所有问题以及相关技巧,敬请期待. SWT开发 JFace开 ... 
- linux上部署应用
			1.编写traffic.sh 引入相关的jar包及java环境路径 2.crontab -e 加入: */10 * * * * cd /opt/sys/traffic_version/bin & ... 
- DJANGO的requirements的运用
			这里记录一下我现在项目的requirements.pip文件,安装命令为: pip install -r requirements.pip 这样一来,所有依赖,全部搞定. Django== djang ... 
- ubuntu安装google 输入法
			12.04 LTS Precise sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4 sudo apt-get in ... 
- 可持久化trie 学习总结
			QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ... 
- [topcoder]HappyLetterDiv2
			http://community.topcoder.com/stat?c=problem_statement&pm=13245 就是有字符串,里面的字符可以随意两两消除,如果不等的话,那么最后 ... 
- 我30天在Stack Overflow问答网站上回答问题的感受
			想法的萌芽 如果非要总结下我多年来是如何使用Stack Overflow的话,我的答案就是:打开网页,搜索问题,查看Stack Overflow的搜索结果,参考答案,最后再关掉网页. 我的生活已经离不 ... 
- React 性能调优原理
			一.React影响性能的两个地方 二.调优原理 
- QC、IQC、IPQC、FQC、OQC
			品质政策为:全面品管.贯彻制度.提供客户需求的品质:全员参与.及时处理.以达成零缺点的目标. 品质三不政策为:不接受不良品.不制造不良品.不流出不良品. QC即英文QUALITY CONTROL的简称 ... 
