题目

83. 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.

解析

class Solution_83 {
public:
ListNode *deleteDuplicates(ListNode *head) { if (!head||!head->next)
{
return head;
} ListNode* cur = head;
ListNode*pre = NULL;
while (cur&&cur->next)
{
pre = cur;
cur = cur->next;
ListNode* temp = pre; //记录每次重复点的开始位置
while(cur&&pre->val==cur->val)
{
pre = cur;
cur=cur->next;
}
temp->next = cur; //跳过重复位置
}
return head;
}
};

82. 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.

解析

  • 由于链表开头可能会有重复项,被删掉的话头指针会改变,而最终却还需要返回链表的头指针。所以需要定义一个新的节点,然后链上原链表,然后定义一个前驱指针和一个现指针,每当前驱指针指向新建的节点,现指针从下一个位置开始往下遍历,遇到相同的则继续往下,直到遇到不同项时,把前驱指针的next指向下面那个不同的元素。如果现指针遍历的第一个元素就不相同,则把前驱指针向下移一位。

//参考容易理解一些
ListNode *deleteDuplicates(ListNode *head) {
if (!head || !head->next) return head; ListNode *start = new ListNode(0);
start->next = head;
ListNode *pre = start;
while (pre->next) {
ListNode *cur = pre->next;
while (cur->next && cur->next->val == cur->val) cur = cur->next;
if (cur != pre->next) pre->next = cur->next;
else pre = pre->next;
}
return start->next;
} // 82. Remove Duplicates from Sorted List II
class Solution_82 {
public:
ListNode* deleteDuplicates(ListNode* head) { if (!head||!head->next)
{
return head;
} ListNode*newHead = new ListNode(0);
newHead->next = head; ListNode* pre = newHead;
ListNode* cur = head; while (cur&&cur->next)
{
ListNode* next = cur->next; if(next->val!=cur->val)
{
if (pre->next==cur) //pre->next当前元素开始,cur当前元素结束,cur->next另外不同的元素
{
pre = cur;
}
else
{
pre->next = cur->next;
}
}
cur = cur->next;
}
if (pre->next!=cur) //这里是地址比较,若没有重复元素,则地址相同的
{
pre->next = cur->next;
}
return newHead->next;
}
};

题目来源

82. Remove Duplicates from Sorted List II && i的更多相关文章

  1. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  2. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

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

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

  4. LeetCode OJ 82. Remove Duplicates from Sorted List II

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

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

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

  6. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  7. [LeetCode#82]Remove Duplicates from Sorted Array II

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

  8. 82. Remove Duplicates from Sorted List II

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

  9. leetcode 82. Remove Duplicates from Sorted List II

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

随机推荐

  1. python中的计时器:timeit

    python中的计时器:timeit timeit 通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:timeit #导 ...

  2. C#字符串(Sring)操作

    //字符串访问            //string s = "ABCD";            //Console.WriteLine(s[0]);//第0位字符       ...

  3. Anaconda 安装 OpenCV 遇到的问题

    1. 使用 pip install   安装 OpenCV 2. 对于 Ananconda 安装 OpenCV ,通常会遇到无法 import 的情况, 这是由于 anaconda 本身没有遵循 PE ...

  4. 洛谷P1558 色板游戏 [线段树]

    题目传送门 色板游戏 题目背景 阿宝上学了,今天老师拿来了一块很长的涂色板. 题目描述 色板长度为L,L是一个正整数,所以我们可以均匀地将它划分成L块1厘米长的小方格.并从左到右标记为1, 2, .. ...

  5. Android之 广播

    (以下内容是阅读郭霖大神的<第一行代码>后自己总结的) 1.概述 广播是Android的四大组件之一. Android的广播机制十分灵活. 2.发送广播 如上图Android的广播主要分为 ...

  6. 深入理解正则表达式-----应用于检测csrf的正则表达式

    如何写检测和防御csrf的规则?我们可以利用正则表达式进行匹配.对POST包进行正则匹配,这里只是提供了一个思路. pcre:"/POST \/(?P<uri>.*?) HTTP ...

  7. 磁盘镜像分析工具TSK

    磁盘镜像分析工具TSK   TSK(The Sleuth Kit)是一款基于命令行的数字取证工具集,用于分析磁盘镜像.该工具支持常见的各种文件系统,如Ext2/Ext3/Ext4.Fat/exFat. ...

  8. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  9. FastReport.Net使用:[11]公共对象属性介绍

    公共对象属性介绍 1.Left(左),Top(上),Height(高度),Width(宽度) Left和Top,用来控制对象的位置:Height和Width用来控制对象的大小. 2.Anchor(基准 ...

  10. Circular dependencies cannot exist in RelativeLayout

    循环布局错误!!! <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...