LeetCode:Remove Duplicates from Sorted List

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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL || head->next == NULL)return head;
ListNode *index = head, *p = head->next, *pre = head;
while(p != NULL)
{
if(p->val != pre->val)
{
index = index->next;
index->val = p->val;
}
pre = p;
p = p->next;
}
p = index->next;
index->next = NULL;
while(p != NULL)
{//销毁多余的节点
ListNode *tmp = p;
p = p->next;
delete tmp;
}
return head;
}
};

LeetCode:Remove Duplicates from Sorted List II

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.                                                  本文地址

/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)return head;
//为操作方便添加头结点
ListNode *addHead = new ListNode();
addHead->next = head;
//index 指向已经保存的最后一个节点
ListNode *index = addHead, *p = head;
while(p != NULL)
{
if(p->next == NULL || p->val != p->next->val)
{//当前节点没有重复
index = index->next;
index->val = p->val;
p = p->next;
}
else
{//当前节点有重复,找到当前节点的最后一个副本的下一个元素
while(p->next && p->val == p->next->val)
p = p->next;
p = p->next;
}
}
p = index->next;
index->next = NULL;
while(p != NULL)
{//销毁多余的节点
ListNode *tmp = p;
p = p->next;
delete tmp;
}
head = addHead->next;
delete addHead;
return head;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461481.html

LeetCode:Remove Duplicates from Sorted List I II的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted Array I II

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

  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 List II 解题报告

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

  5. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  6. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  7. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

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

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

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

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

随机推荐

  1. windows 和 linux ssh互连

    从windows连接到linux: linux开启sshd服务即可,主要是windows的配置如下: 1.使用软件,putty可以直接使用 2.使用cmd控制台连接linux,安装SSH Secure ...

  2. Windows Server 2012之搭建域控制器DC

    安装域控制器,域(Domain) 1,本地管理员权限 2,设置静态IP 地址 3,至少有一个NTFS分区 4,操作系统版本(web版除外)   设置静态IP地址    dcpromo.exe命令不生效 ...

  3. linux 查看系统版本

    博客分类: linux LinuxRedHatDebianSuSE  几种查看linux版本信息的方法: uname -a cat /proc/version cat /etc/issue lsb_r ...

  4. ARP协议的报文格式

    原文链接地址:http://www.cnblogs.com/laojie4321/archive/2012/04/12/2444187.html   结构ether_header定义了以太网帧首部:结 ...

  5. hibernate一对一关系实现

    按照主键映射,按照外键映射 Address.hbm.xml: <?xml version="1.0"?><!DOCTYPE hibernate-mapping P ...

  6. 一个不错的shell 脚本教程 入门级

    一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂     建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行s ...

  7. ELK 信息统计分析-1

    Aggregations 格式如下: "aggregations"{ //可以简写为aggs "<aggregation_name>":{ //名称 ...

  8. Raphael实现商品来源去向图

    数据可视化 是很多大数据分析的一项重要工作,甚至有专门的团队做这项工作.Web上的各种图形(饼状图,柱状图等)一直被flash所垄断,随着HTML5的发展,SVG和Canvas也逐渐走上舞台.这不,产 ...

  9. Uva-11374-Airport Express

    A - Airport Express Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Appoi ...

  10. jQuery 获取页面元素的属性值

    获取浏览器显示区域(可视区域)的高度 :    $(window).height();    获取浏览器显示区域(可视区域)的宽度 : $(window).width();    获取页面的文档高度 ...