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. Jquery DataTables相关示例

    一.Jquery-DataTables DataTables 是jquery的一个开源的插件.它具有高度灵活的特性,基于渐进增强的基础,可以为任何表格添加交互.它特性如下: 提供分页,搜索和多列排序: ...

  2. emacs24下使用jedi对python编程进行补全

    在开始前先装好pip和virtualenv(见pip的安装一文),另需安装好make 1.emacs下安装: epc deferred.el auto-complete 使用M-x package-i ...

  3. hibernate集合类型映射

    Set无序 元素不可重复 List有序 元素可重复 Bag无序 元素可重复 Map键值对 Student: package model; import java.util.Set; public cl ...

  4. 修改mysql默认字符集的方案

    mysql默认字符集能否进行修改呢?答案是肯定的,下面就将教您两种修改mysql默认字符集的方法,希望对您学习mysql默认字符集方面能有所启迪. (1) 最简单的修改方法,就是修改mysql的my. ...

  5. FAQ-Ubuntu12.04 15.04禁止移动介质自动播放

    网上有有很多关于Ubuntu10.04关闭移动介质自动播放的方法,包括在文件管理器里面设置或者使用gconf-editor,但是从12.04开始这两种方法都不再好用了,关于移动介质的处理方法被移到了S ...

  6. Linux学习之一——开机必备知识

    linux最重要的特色是指令交互,利用一个个指令来进行各种操作.当然可以用X Window获得友好的linux窗口,但是还是有很多局限性. linux用户登录后默认进入 /root目录下. 比较重要的 ...

  7. JasperReports教程:Report Data Sources

    原文地址:http://www.tutorialspoint.com/jasper_reports/jasper_report_data_sources.htm Datasources是一个结构化的数 ...

  8. 如何做好presentation

    1.全心投入 要么不做,要做就做好 承诺自己会花时间好好准备自己的演讲,投入专注的精力. 人们可以通过练习使自己成为很好的演讲者. 2分析你的观众 他们想听什么? 3.组织你的想法 让语言简单 让观众 ...

  9. JVM相关问答

    大部分内容来源网络,整理一下,留个底. 问:堆和栈有什么区别? 答:堆是存放对象的,但是对象内的临时变量是存在栈内存中,如例子中的methodVar是在运行期存放到栈中的. 栈是跟随线程的,有线程就有 ...

  10. WEB安全--CSRF剖析

    CSRF攻击:攻击者构造合法的HTTP请求,随后利用用户的身份操作用户帐户的一种攻击方式. 一.CSRF攻击原理CSRF的攻击建立在浏览器与Web服务器的会话中:欺骗用户访问URL.二.CSRF攻击场 ...