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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *p;
p = head;
if(!p) return head; while(p->next){
if(p->next->val == p->val){
ListNode *q;
q=p->next;
p->next = q->next;
delete q;
}
else{
p = p->next; }
} return head;
}
};

思路:考察链表的基本操作,只要注意细节就没问题。

[leetcode.com]算法题目 - Remove Duplicates from Sorted List的更多相关文章

  1. [算法题] Remove Duplicates from Sorted Array ii

    题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...

  2. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

  3. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  4. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  5. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. LeetCode算法题-Remove Duplicates from Sorted List

    这是悦乐书的第160次更新,第162篇原创 01 前情回顾 昨晚的爬楼梯算法题,有位朋友提了个思路,使用动态规划算法.介于篇幅问题,这里不细说动态规划算法,以后会在数据机构和算法的理论知识里细说. 昨 ...

  8. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  9. [算法题] Remove Duplicates from Sorted Array

    题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...

随机推荐

  1. IOS初级:UIView和UIButton

    AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...

  2. POI导出大量数据的简单解决方案

    说明:我的电脑 2.0CPU 2G内存 能够十秒钟导出 20W 条数据 ,12.8M的excel内容压缩后2.68M 我们知道在POI导出Excel时,数据量大了,很容易导致内存溢出.由于Excel ...

  3. regular exception

    RegexBuddy :正则表达式编辑器. 正则表达式:通过特定的规则,选取特定的字符串. ^ 为匹配输入字符串的开始位置.$ 为匹配输入字符串的结束位置. \d匹配[0-9] \w匹配[A-Z a- ...

  4. 674. Longest Continuous Increasing Subsequence

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  5. idea运行项目时报Error:java无效的源发行版:1.8

    如果你安装的是JDK1.7,而在file->project structure中设置的是language level是8的话,就会出现这个错误提示:无效的源发行版:8. 解决办法:将语言级别改为 ...

  6. jdk1.6的配置

    执行下列命令安装jdk1.7 /usr/lib/jvm$ sudo apt-get install openjdk-7-jdk openjdk-7-jre 会得到下面这两个文件: 查看当前jdk版本: ...

  7. 为WinEdt自定义XeLatex快捷键

    没有彻底抛弃Windows很重要的一方面原因,WinEdt + Sumatra PDF对LaTeX支持的太好了(自动补全,反向搜索),而且当遇到复杂公式的时候,mathtype也能帮上大忙. 我一直用 ...

  8. bzoj4873(最大权闭合子图)

    今天学了最大权闭合子图..然后找了这道题,发现完全不会..... 看了题解发现这种有诸如选了一个就一定要选另外的一些的限制又要求最优值的题有的可以转化成最大权闭合子图, 这个题我们首先想到不会选相交的 ...

  9. Leetcode--136. Single Number(easy)

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  10. 【20171101】the first day in a new company

    英文差的要命还飙英文,擦嘞!!! 就是想记录下 点滴 如下配图: | | | V 手动配图这是图!!!