博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode 70] 82 Remove Duplicates from Sorted List II
阅读量:5299 次
发布时间:2019-06-14

本文共 2791 字,大约阅读时间需要 9 分钟。

Problems:

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.

 

Analysis:

This problem is a bit different from the classic probelm of removing duplicates. Here we need to delete all nodes of duplication. To achieve this, three pointers and a flag are needed.

ListNode *ppre is used to point to the node that before dup;

ListNode *pre is used to point to the first node of the duplication;

ListNode *cur is used to point to the current checking node;

bool isDup is used to indicate whether there's duplication;

The checking process is as follows:

1. first check whther pre.val equals cur.vla or not; if equal go to step 2 else go to step 3

2. remove the cur node, and set the isDup flag; go back to step 1;

3. check whether isDup is set or not; if set, delete the pre node. otherwise move all three pointers one node further.

The space needed is constant and the time complexity is O(n).

 

One thing to remember is that after exitting the while loop, we need an extra check of isDup to decide whether delete or not the last node of the list

 

Code:

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         // Start typing your C/C++ solution below13         // DO NOT write int main() function14         if (head == NULL || head->next == NULL)15             return head;16         17         ListNode dmy(-1);18         dmy.next = head;19         20         ListNode *ppre = &dmy, *pre = head, *cur = head->next;21         bool isDup = false;22         23         while (cur != NULL) {24             if (cur->val == pre->val) {25                 isDup = true;26                 pre->next = cur->next;27                 cur->next = NULL;28                 delete cur;29                 cur = pre->next;30             } else {31                 if (isDup) {32                     ppre->next = pre->next;33                     pre->next = NULL;34                     delete pre;35                     pre = cur;36                     cur = cur->next;37                     isDup = false;38                 } else {39                     ppre = pre;40                     pre = cur;41                     cur = cur->next;42                 }43             }44         }45         46         if (isDup) {47             ppre->next = pre->next;48             pre->next = NULL;49             delete pre;50         }51             52         return dmy.next;53     }54 };
View Code

 

转载于:https://www.cnblogs.com/freeneng/p/3203373.html

你可能感兴趣的文章
Notepad++ 16进制编辑功能
查看>>
Caffe: Cannot create Cublas handle. Cublas won't be available
查看>>
Linux 下 LXD 容器搭建 Hadoop 集群
查看>>
mysql describe
查看>>
apache自带压力测试工具ab的使用及解析
查看>>
C语言作业3
查看>>
C#使用Xamarin开发可移植移动应用(2.Xamarin.Forms布局,本篇很长,注意)附源码
查看>>
koogra--Excel文件读取利器
查看>>
ASP.NET 使用ajaxupload.js插件出现上传较大文件失败的解决方法
查看>>
jenkins搭建
查看>>
C#中使用Split分隔字符串的技巧
查看>>
(springboot)freemarker(二)
查看>>
linux下golang gRPC配置详解
查看>>
mongodb 简单使用说明
查看>>
eclipse的调试方法的简单介绍
查看>>
OneAPM 云监控部署与试用体验
查看>>
加固linux
查看>>
wget 升级
查看>>
为什么需要大数据安全分析?
查看>>
day13.字典复习
查看>>