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(0);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

转载于:https://www.cnblogs.com/TenosDoIt/p/3461481.html

LeetCode:Remove Duplicates from Sorted List I II相关推荐

  1. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

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

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

  3. [LeetCode] Remove Duplicates from Sorted Array II

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

  4. LeetCode Remove Duplicates from Sorted List II

    题意:给出一个单链表 ,将其中重复的元素删除 思路:在找重复结点时,需要找到其前继结点 代码如下: class Solution {public ListNode deleteDuplicates(L ...

  5. LeetCode() Remove duplicates from sorted list II

    ListNode* dummy = new ListNode(0); //必须要加上 new ListNode(0); 否则有错误. dummy->next = head; head = dum ...

  6. LeetCode Remove Duplicates from Sorted List

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

  7. [LeetCode] Remove Duplicates from Sorted List - 链表问题

    题目概述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  8. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  9. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

最新文章

  1. C语言中的输入输出函数
  2. RoFormerV2:自然语言理解的极限探索
  3. java 数据库改操作_数据库的插入、修改、删除操作(java实现)
  4. 0和5(51Nod-1433)
  5. 让服务器自动从HG版本库中下载代码
  6. paip.提升效率--数据绑定到table原理和流程Angular js jquery实现
  7. Docker存储驱动之总览
  8. 图解设计模式:行为型模式之责任链模式
  9. java带圈数字,小1,小2
  10. Firebug 入门指南
  11. 三角形的几何公式大全_解析几何(椭圆)常见二级结论92条附详细证明
  12. 热血江湖数据库MYSQL修改_热血江湖数据库修改终极教程-20070823更新
  13. CANoe如何查看总线负载率?
  14. BLE蓝牙连接不稳定以及突然断开的原因和解决方法
  15. IOS 多线程之信号量
  16. 曾扬言 机器人合法公民_简直荒谬!这个机器人被授予合法公民身份,并公开嘲笑马斯克!...
  17. 定义一个学生类Student,包含三个属性姓名、年龄、性别, 创建三个学生对象存入ArrayList集合中。 A:使用迭代器遍历集合。 B:求出年龄最大的学生,然后将该对象的姓名变为:小猪佩奇。
  18. intel clear linux 教程,英特尔ClearLinux安装教程
  19. 面向协议与面向对象的区别
  20. 电脑开机自检的过程和屏显

热门文章

  1. 数据结构系列(四)栈与队列
  2. WebService学习总结(二)——WebService相关概念介绍
  3. 菜鸟谈算法和数学对写程序的影响
  4. 发行版迁移及二进制兼容性注意事项
  5. eigrp hello报文格式
  6. 用oracle的java存储过程实现BLOB字段的字符串读取
  7. Java多线程(线程同步)
  8. 心胸狭窄小肚鸡肠的男人_为心胸开阔的教育者编写新的剧本
  9. 云原生应用程序_什么是云原生应用程序?
  10. 戴尔电脑正在准备自动修复_戴尔如何建立社区为开放的未来做准备