82. Remove Duplicates from Sorted List II【Medium】

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.

解法一:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* deleteDuplicates(ListNode* head) {
  12. if (head == NULL || head->next == NULL) {
  13. return head;
  14. }
  15.  
  16. ListNode * dummy = new ListNode(INT_MIN);
  17. dummy->next = head;
  18. head = dummy;
  19. int duplicate;
  20.  
  21. while (head->next != NULL && head->next->next != NULL) {
  22. if (head->next->val == head->next->next->val) {
  23. duplicate = head->next->val;
  24. while (head->next != NULL && head->next->val == duplicate) {
  25. ListNode * temp = head->next;
  26. free(temp);
  27. head->next = head->next->next;
  28. }
  29. }
  30. else {
  31. head = head->next;
  32. }
  33. }
  34. return dummy->next;
  35.  
  36. }
  37. };

nc

解法二:

  1. class Solution {
  2. public:
  3. ListNode* deleteDuplicates(ListNode* head) {
  4. if(!head) return head;
  5. ListNode dummy();
  6. dummy.next = head;
  7. ListNode *pre = &dummy;
  8. ListNode *cur = head;
  9.  
  10. while(cur && cur->next){
  11. while(cur->next && cur->val == cur->next->val) cur = cur->next;
  12. if(pre->next == cur){
  13. pre = cur;
  14. cur = cur->next;
  15. } else {
  16. cur = cur->next;
  17. pre->next = cur;
  18. }
  19. }
  20.  
  21. return dummy.next;
  22. }
  23. };

参考了@liismn 的代码

解法三:

  1. class Solution {
  2. public:
  3. ListNode* deleteDuplicates(ListNode* head) {
  4. if(!head||!head->next) return head;
  5. ListNode* dummy = new ListNode();
  6. ListNode* tail = dummy;
  7. int flag = true; // should the current head be added ?
  8. while(head){
  9. while(head&&head->next&&head->val==head->next->val)
  10. {
  11. flag = false; // finds duplicate, set it to false
  12. head = head->next;
  13. }
  14. if(flag) // if should be added
  15. {
  16. tail->next = head;
  17. tail = tail->next;
  18. }
  19. head = head->next;
  20. flag = true; // time for a new head value, set flag back to true
  21. }
  22. tail->next = nullptr; // Don't forget this... I did..
  23. return dummy->next;
  24. }
  25. };

参考了@GoGoDong 的代码

解法四:

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if (head == null) return null;
  3.  
  4. if (head.next != null && head.val == head.next.val) {
  5. while (head.next != null && head.val == head.next.val) {
  6. head = head.next;
  7. }
  8. return deleteDuplicates(head.next);
  9. } else {
  10. head.next = deleteDuplicates(head.next);
  11. }
  12. return head;
  13. }

递归,参考了@totalheap 的代码,还不太明白

82. Remove Duplicates from Sorted List II【Medium】的更多相关文章

  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 && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

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

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

  4. [LeetCode] 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 OJ 82. Remove Duplicates from Sorted List 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 移除有序链表中的重复项 II

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

  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. 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【Leetcode】82. Remove Duplicates from Sorted List II

    Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...

随机推荐

  1. 【单调队列优化DP】BZOJ1855-[Scoi2010]股票交易

    [题目大意] 已知第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每个i,都有APi>=BPi),第i天的一次买入至多只能购买ASi股,一次卖出至多只能卖出BSi股. ...

  2. python3 中 and 和 or 运算规律

    一.包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Python中,None.任何数值类型中的0.空字符串“”.空元组().空列 ...

  3. 计算数字出现的次数 Exercise07_03

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:计算数字出现的次数 * */ public class Exercise0 ...

  4. 五角数 Exercise06_01

    /** * @author 冰樱梦 * 题目:五角数 * 时间:2018年下半年 * * */ public class Exercise06_01 { public static void main ...

  5. 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)

    专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping

  6. 调试SQLSERVER (二)使用Windbg调试SQLSERVER的环境设置 ------符号文件

    http://www.cnblogs.com/lyhabc/p/4184708.html

  7. 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代

    package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...

  8. 让IE浏览器支持HTML5

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  9. coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1

    因为最近要做不同分辩率的适配,所于看了下引擎这方面的代码,记录一下当是学习笔记,cocos2d-x 版本 2.2.2 , 例子是samples\Cpp\TestCpp下的 TestCpp. 先看下ma ...

  10. apache poi合并单元格设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...