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. select_tag in rails about selected not change and onchange()

    make it more easy after http://www.cnblogs.com/juandx/p/4058399.html 1  if you want the selected is ...

  2. Spring中Template模式与callback的结合使用浅析

    Spring不论是与ibatis,还是与Hibernate的结合中,都使用到了Template模式与callback技术,来达到简化代码实现的目的.Template模式也即模板模式,用于对一些不太变化 ...

  3. SQLAlchemy 中文文档翻译计划

    SQLAlchemy 中文文档翻译计划已启动. Python 文档协作翻译小组人手紧缺,有兴趣的朋友可以加入我们,完全公益性质.交流群:467338606. 希望大家能够勇敢地去翻译和改进翻译.虽然我 ...

  4. 【node.js】安装express后,'express' 不是内部或外部命令的问题

    因express默认安装是最新的版本,已经是4.x.x的版本.而最新express4.0+版本中将命令工具分出来了,所以必须要安装express-generator,执行: D:\TOOLS\Node ...

  5. 根据SQL Server排序规则创建顺序GUID

    public static class GuidUtil { , , , , , , DateTimeKind.Utc).Ticks / 10000L; /// <summary> /// ...

  6. SQOOP Load Data from Oracle to Hive Table

    sqoop import -D oraoop.disabled=true \ --connect "jdbc:oracle:thin:@(description=(address=(prot ...

  7. 【实践】获取CKEditor的html文本、纯文本、被选中的内容及赋值

      <%=Html.TextAreaFor(Model => Model.WORK_INTRODUCTION)%> <script type="text/javasc ...

  8. Ngrok远程桌面及ssh配置

    上一篇Ngrok 内网穿透利器 使用教程我们讲到Ngrok的基本使用教程,这篇描述一下Ngrok的远程桌面及ssh配置 Step 1 修改配置文件ngrok.cfg server_addr: &quo ...

  9. BZOJ 1500 维修数列【Splay】

    注意:1,内存限制,所以需要回收删除的点 2,当前节点的左连续区间和最大值=max(左子树的左连续区间和最大值,左子树的总和+当节点的值+max(右子树的左连续区间和最大值,0)):右连续区间和最大值 ...

  10. Eclipse去除JavaScript验证错误

    这篇文章主要是对Eclipse去除js(JavaScript)验证错误进行了介绍.在Eclipse中,js文件常常会报错.可以通过如下几个步骤解决 第一步: 去除eclipse的JS验证: 将wind ...