题目

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. 牛客练习赛19 C-托米航空公司

    思路:轮廓线dp,找bug找死我了. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  2. 那些年遇到的php之坑

    1. php指针没有重置 $arr = array( array('aaaaaaaa'), array('bbbbbbb') ); unset($arr[0]); unset($arr[1]); so ...

  3. 【C++初级】static用法总结、问题探讨及常见错误排查

    static的基本用法: static的作用主要有两种第一个作用是限定作用域:第二个作用是保持变量内容持久化: 一.c语言中static的用法: 1.全局静态变量: 用法:在全局变量前加上关键字sta ...

  4. BZOJ 3998: [TJOI2015]弦论 后缀自动机 后缀自动机求第k小子串

    http://www.lydsy.com/JudgeOnline/problem.php?id=3998 后缀自动机应用的一个模板?需要对len进行一个排序之后再统计每个出现的数量,维护的是以该字符串 ...

  5. Problem F: 最大公约数、最小公倍数

    Description 输入两个正整数m和n,输出m.n的最大公约数和最大公倍数.先计算最大公约数,m和n得乘积除以最大公约数,就得到了最小公倍数.其中最大公约数可以用穷举法求得,也可以用辗转相除法求 ...

  6. 25.最小生成树(kruskal算法)

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立 ...

  7. 如何解决…has been modified since the precompiled header… was built的问题

    如何解决…has been modified since the precompiled header… was built 的问题 xcode5.1在程序中报错: File '/Applicatio ...

  8. Circuit forms adjustable bipolar clamp

    The easy way to clamp a signal to a given value is to use two zener diodes, connected back-to-back. ...

  9. Maven编译代码的相关命令

    第一.main目录下的主代码编写完毕后,使用Maven进行编译,在项目根目录下运行命令mvn clean compile进       行项目编译. 第二.test目录下的测试用例编写完毕之后就可以调 ...

  10. SQLSERVER里面RR隔离级别没有GAP锁

    http://www.cnblogs.com/MYSQLZOUQI/articles/3862721.html