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.

解法一:

 /**
* 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) {
if (head == NULL || head->next == NULL) {
return head;
} ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head;
head = dummy;
int duplicate; while (head->next != NULL && head->next->next != NULL) {
if (head->next->val == head->next->next->val) {
duplicate = head->next->val;
while (head->next != NULL && head->next->val == duplicate) {
ListNode * temp = head->next;
free(temp);
head->next = head->next->next;
}
}
else {
head = head->next;
}
}
return dummy->next; }
};

nc

解法二:

 class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return head;
ListNode dummy();
dummy.next = head;
ListNode *pre = &dummy;
ListNode *cur = head; while(cur && cur->next){
while(cur->next && cur->val == cur->next->val) cur = cur->next;
if(pre->next == cur){
pre = cur;
cur = cur->next;
} else {
cur = cur->next;
pre->next = cur;
}
} return dummy.next;
}
};

参考了@liismn 的代码

解法三:

 class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head||!head->next) return head;
ListNode* dummy = new ListNode();
ListNode* tail = dummy;
int flag = true; // should the current head be added ?
while(head){
while(head&&head->next&&head->val==head->next->val)
{
flag = false; // finds duplicate, set it to false
head = head->next;
}
if(flag) // if should be added
{
tail->next = head;
tail = tail->next;
}
head = head->next;
flag = true; // time for a new head value, set flag back to true
}
tail->next = nullptr; // Don't forget this... I did..
return dummy->next;
}
};

参考了@GoGoDong 的代码

解法四:

 public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null; if (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val) {
head = head.next;
}
return deleteDuplicates(head.next);
} else {
head.next = deleteDuplicates(head.next);
}
return head;
}

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

最新文章

  1. Spring mvc
  2. 参数化命令相关知识点之==================防止SQl的注入
  3. 遍历Map
  4. ExtJs文件上传(Ext.ux.form.FileUploadField)
  5. Codeforces Round #382 (Div. 2)B. Urbanization 贪心
  6. Spring Autowiring by AutoDetect
  7. HDU 1562 Guess the number
  8. 学习笔记:javascript 文档对象(document)
  9. TCP/IP协议全解析 三次握手与四次挥手[转]
  10. Hive-ORC文件存储格式(续)
  11. tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署
  12. Netty、t-io、Voovan 框架比较
  13. macos + vs code + grep 进行多文件搜索
  14. ASP.NET C# 实现实时用户在线
  15. 剑指Spring源码(一)
  16. sqoop的安装
  17. PHP+Ajax实现文件上传功能
  18. noip第27课资料
  19. 使用linux的shell脚本实现在当前行重复动态显示时间等字符串信息(不另起新行)
  20. Unity3d资源管理分析

热门文章

  1. 【spfa】bzoj3921 Mimori与树海
  2. iptables禁止外网访问redis server服务默认端口6379的命令
  3. 【mybatis】mybatis中insert 主键自增和不自增的插入情况【mysql】
  4. centos7 ping127.0.0.1不通
  5. CreatarGlobe实现多机立体显示方案(初稿)
  6. yii2 URL重写 nginx的配置
  7. Spark Streaming揭秘 Day2-五大核心特征
  8. go 中goroutine 的使用
  9. angular—— Dynamic Templates
  10. virtualbox 设置windows 于ubuntu虚拟机共享文件夹