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) {
if(head == NULL) return NULL;
ListNode *cur = head;
ListNode *next = head->next;
while(next != NULL)
{
if(next->val == cur->val)
{
cur->next = next->next;
delete next;
next = cur->next;
}
else
{
cur = cur->next;
next = next->next;
}
}
return head;
}
};

Leetcode41: Remove Duplicates from Sorted List的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

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

  7. Remove Duplicates From Sorted Array

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

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. linux中test的意义 又可以表示为[]

    测试标志 代表意义 文件名.文件类型 -e 该文件名是否存在 -f 该文件名是否存在且为file -d 该文件名是否存在且为目录 -b 该文件名是否存在且为一个block -c 该文件名是否存在且为一 ...

  2. 数据库---大数据+hadoop

    大数据:hadoop:大数据和hadoop的关系

  3. 练习题,新建数据库anyun

    一.新建数据库,数据库名为anyun mysql> create database anyun; Query OK, 1 row affected (0.00 sec) 二.查看数据库 mysq ...

  4. redis主从复制以及SSDB主主复制环境部署记录(四)

    redis配置主从和ssdb主主复制 参考:散尽浮华Linux下Redis主从复制以及SSDB主主复制环境部署记录 只做自己查阅. 环境要求:三台虚拟机一台做主,两台做从. 通过SSDB在redis做 ...

  5. Quartz与Terracotta的集成

    Quartz与Terracotta的集成 博客分类: Java&Socket 企业应用面临的问题 开源组件的应用 quartz任务集群数据库任务调度Terracotta  Quartz与Ter ...

  6. BZOJ 1426 收集邮票 ——概率DP

    $f(i)$表示现在有$i$张,买到$n$张的期望 所以$f(i)=f(i+1)+\frac {n}{n-i}$ 费用提前计算,每张邮票看做一元,然后使后面每一张加1元 $g(i)$表示当前为$i$张 ...

  7. TeraTerm设定(解决日文乱码问题)

    首先,字体Font的MS Gothic是有Japanese的,设置为这个比较保险. 其次,在General Setup里将Language设为:English. 原理是什么我也不清楚,试了几个选择,就 ...

  8. iOS推送的开启与关闭

    开启: 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationType ...

  9. shell的case脚本的简单入门

    shell的case脚本的简单入门 示例1: #/bin/bash a=$ case "$a" in ") echo 'hell 2';; ") echo 'h ...

  10. Yii 之视图数据块

    控制器代码: public $layout = 'common'; public function actionStudent(){ $data = array('page_name'=>'St ...