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的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. 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 ...

  4. 【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 ...

  5. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

随机推荐

  1. SVN库实时同步设置

    为了安全起见,SVN服务器除了做定时全量备份和增量备份以外,如条件允许,可做实时备份. 这需要2台机器,一台是master server,另一台做mirror server.master做主服务,mi ...

  2. httpsClient实例

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  3. Git PHP提交

    做了个小的DEMO,可以查看: https://github.com/feixiang/webgit.git 这几天一直在郁闷的事情. Git在shell里面执行得好好的,apache运行用户也改成了 ...

  4. HDU 1695 GCD (容斥原理+欧拉函数)

    题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...

  5. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  6. linux内核--进程与线程

    http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...

  7. Delphi判断字符串是否是数字、字母、大小写字母

    function IsNumberic(Vaule:String):Boolean; //判断Vaule是不是数字 var i:integer; begin result:=true; //设置返回值 ...

  8. JavaMail如何保证邮件发送成功

    使用过JavaMail的api发送邮件的人可能会有这样一个疑惑:我如何知道我调用该api发送的邮件是否成功呢?一般的开放的api给我们调用都会有个返回值或者状态码,来告诉我们执行成功与否.但是Java ...

  9. Tomcat下的一些配置

    1. JAVA虚拟机性能优化,修改bin下的  catalina.sh/bat rem ----- Execute The Requested Command -------------------- ...

  10. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...